1

Is there a way in WordPress that I can hide say Update Notices from other administrators unless there username is equal to say main-admin?

Essentially I want something like this:

if ( username != 'main-admin' ) {
    // Hide all updates
}

Is there a way to do this at all?

3

You could try following:

$current_user = wp_get_current_user();
if( user_can( $current_user, 'main-admin' ) ) {
    // Do your stuff
}

For more info please read in the Codex about:
user_can , wp_get_current_user

Edit: my mistake, was reading wrong, I thought you meant a custom role main-admin.
Not to make my mistake suddenly as correct, but maybe you create a role where the user belongs to. (is maybe even easier to manage?)


Add-on Trying to give an correct answer/option now.

There are several ways/options to achieve your goal below are 2:

Knowing the name makes it able to use: get_currentuserinfo()

global $current_user;
get_currentuserinfo();
if ( $current_user->user_lastname ) {
    // Do your stuff
}

Codex: get_currentuserinfo

When you know the user id you could use get_current_user_id():

$user_id = get_current_user_id();
if ( $user_id == 5 ) {  // add correct id
    // Do your stuff 
}

Codex: get_current_user_id


As mentioned you can also create a custom role with administrator caps, which is explained here by @RutwickGangurde. To keep the answer here complete I added the code here.
NB: The setting is saved to the database (in table wp_options, field wp_user_roles), so it needs to run only once

I personally use a Class in functions.php to be sure a certain function only runs once, which in this case is a must!
Add both functions into your functions.php (make first a copy!)

class run_once
{
    function run( $key )
    {
        $scenario = get_option( 'run_once' );
        if ( isset( $scenario[$key] ) && $scenario[$key] )
        {
            return false;
        }
        else {
            $scenario[$key] = true;
            update_option( 'run_once', $scenario );
            return true;
        }
    } // end function
} // end class

// Now use the class with this function (which will only run once!)
$run_once = new run_once;
if ( $run_once->run( 'cloneRole' ) )
{       
    function cloneRole()
    {
        global $wp_roles;

        if ( ! isset( $wp_roles ) )
        $wp_roles = new WP_Roles();

        $adm = $wp_roles->get_role( 'administrator' );
        //Adding a 'new_role' with all admin caps
        $wp_roles->add_role( 'main-admin', 'Main Admin', $adm->capabilities );
    } // end Function
    add_action( 'init', 'cloneRole' );    
} // end if(..)

Core info about add_role

You can now add users to this custom role and use that role in other situations as in:

$current_user = wp_get_current_user();
if( user_can( $current_user, 'main-admin' ) )
{
    // Do your stuff
}
  • I will give it a try :) – Stephen Oct 6 '16 at 10:42
  • Might be more robust to use a custom capability and then give that capability to this particular admin account. – Andy Macaulay-Brook Oct 6 '16 at 11:44

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.