I'm attempting to build a locked page on wordpress (through a plugin) that shows differant data, depending on who signs-in. Essentially it has three screens, as follows:

  1. If you sign-in as the admin (or someone with admin privileges granted) it shows a screen that has info and functionallity for the admin only;
  2. If you sign-in as a default user (created by admin) you're taken to a private registration page;
  3. If you sign-in with a registered account you're presented with a file uploader.

The way I'm doing this is with a if elseif else statement; like so if (current_user_can( 'manage_options' )): 1, elseif ( 2 == $current_user->ID ): 2, else: 3, endif;.

What I'm wondering is if the way I'm calling the second function is best practice, or if there is a better/saver way of doing this? Thanks for any help! ;)

  • inside the elseif you test a single id? – gmazzap Nov 8 '13 at 20:39
  • @G.M. Yes, that's what I'm asking... is this best practice? I only want to show the data within the elseif to one user, i.e. "Standard" or something like that. – Designer 17 Nov 8 '13 at 20:43

To check if an user is a specific user you have 4 options:

  • check the 'ID'
  • check the nicename (slug)
  • check the email
  • check the user login

All these 4 fields are unique for every user.

So, check the ID is an option, but is the worst: when create that user admin doesn't know the id, he had look at the ID after he had created user, so you can write your code after the user is created... this is bad.

The other 3 options are a bit (just a bit) better: you can wite the code before the user is created, E.G.:

if ( 'standard_user' === $current_user->user_nicename ) {
  // do something

And after that, the admin have to create the user, but remembering to use 'standard_user' as user nicename.

However, once you ask about best practice, I can't say it's a best practice, because it needs an hardcoding: you write in code something that to work needs the admin when create user type a specific string... and if a typo happen? if admin doesn't recall exactly which is the string to use?

If you are writing the code for yourself, this can be used with no much pain, but if you are writing this for someone else (a customer?) or you plan to distribuite your code, you should surely do it in another way.

There are different possibilities, this is one:

First of all, you have to think which is the role normally you have added to that particular standard user.

Assuming that user should be a contributor.

On plugin activation create a custom role cloning contributor capabilities.

function myplugin_install() {
  $contributor = get_role('contributor');
  $standard = add_role( 'standard', 'Standard User', $contributor->capabilities );

register_activation_hook( __FILE__, 'myplugin_install' );

After that, write your condiction like this:

$user = wp_get_current_user();

if ( user_can($user, 'manage_options') ) {
  // code for admin
} elseif ( in_array( 'standard', $user->roles ) ) {
  // code for your standard user 
} else {
  // code for others

Now, when the administrator create the Standard user have only to choose 'Standard User' from the user role dropdown.

PS: remember to remove your custom role on plugin deactivation:

function myplugin_unistall() {
  remove_role( 'standard' );

register_deactivation_hook( __FILE__, 'myplugin_unistall' );
  • Thanks!!! You hit the nail on the head; that's exactly what I was needing to know. You did a really good job of explaining it. ;) – Designer 17 Nov 8 '13 at 21:54
  • @Designer17 You're welcome. – gmazzap Nov 8 '13 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.