wpsmith/user

WordPress user.

0.0.10 2019-01-09 01:15 UTC

README

This package contains two classes:

  1. CurrentUser
  2. HideUser

Set Current User

WordPress does not set the current user until right before the init hook. This can be problematic sometimes. Sometimes, you may need to set the current user before WordPress is ready to set the current user (e.g., on after_setup_theme, muplugins_loaded, registered_taxonomy or registered_post_type hooks). So this class enables you to do that.

For the current user:

$the_current_user = \WPS\User\CurrentUser::get_instance()->get_current_user();

Determining whether a user is the current user by User ID, email or username:

// By ID.
\WPS\User\CurrentUser::get_instance()->is_current_user( 2 )

// By Email.
\WPS\User\CurrentUser::get_instance()->is_current_user( 'email@domain.com' )

// By username/user login
\WPS\User\CurrentUser::get_instance()->is_current_user( 'myusername' )

You can also use this to set "super" users.

$super_users = array(
    'username1',
    'email@domain.com',
    3,
);
\WPS\User\CurrentUser::get_instance()->is_current_a_super_user()

Hide User

Sometimes it is good to hide a user from other users so that user won't be deleted or modified accidentally by another administrator.

To hide a user or set of users:

\WPS\User\HideUser::get_instance( array(
    'hidden_user1',
    'hidden_user2',
) );