$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Add OR in mu-plugin to check if one of multiple users is the logged in user|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Add OR in mu-plugin to check if one of multiple users is the logged in user

matteradmin10PV0评论

I've searched through WordPress Development and couldn't find the answer, so I either didn't search very well or I couldn't figure out the actual term I needed to search for...

I am trying to create a simple mu-plugin to remove update notices, nags and other random notifications created by many of the plugins I use across a lot of sites for clients. The plugin removes the notices for all except a single user.

The code below works, but I know I'm not removing the core, plugins and themes notifications.

That said, the issue I'm trying to tackle now (to no avail) is to be able to add multiple users who are able to see the notices (by username)... So, AdminUser and AdminUser2 and AdminUser3 should see the notices when they're logged in.

I'm not much of a developer, so any help is much appreciated.

//Remove WordPress nags and notices from the WordPress dashboard for all but one user. REPLACE 'AdminUser' with your username
function hide_wp_dashboard_notices()
{
    $user = wp_get_current_user();

    if($user && isset($user->user_login) && 'AdminUser' !== $user->user_login) {
        echo '<style>.update-nag, .updated, .error, .is-dismissible, .notice.woo-permalink-manager-banner, #try-gutenberg-panel, span.update-plugins, .yoast-issue-counter, table.wp-list-table.plugins .notice-warning { display: none !important; }</style>';
    }
}   
add_action('admin_enqueue_scripts', 'hide_wp_dashboard_notices');
add_action('login_enqueue_scripts', 'hide_wp_dashboard_notices');

I've searched through WordPress Development and couldn't find the answer, so I either didn't search very well or I couldn't figure out the actual term I needed to search for...

I am trying to create a simple mu-plugin to remove update notices, nags and other random notifications created by many of the plugins I use across a lot of sites for clients. The plugin removes the notices for all except a single user.

The code below works, but I know I'm not removing the core, plugins and themes notifications.

That said, the issue I'm trying to tackle now (to no avail) is to be able to add multiple users who are able to see the notices (by username)... So, AdminUser and AdminUser2 and AdminUser3 should see the notices when they're logged in.

I'm not much of a developer, so any help is much appreciated.

//Remove WordPress nags and notices from the WordPress dashboard for all but one user. REPLACE 'AdminUser' with your username
function hide_wp_dashboard_notices()
{
    $user = wp_get_current_user();

    if($user && isset($user->user_login) && 'AdminUser' !== $user->user_login) {
        echo '<style>.update-nag, .updated, .error, .is-dismissible, .notice.woo-permalink-manager-banner, #try-gutenberg-panel, span.update-plugins, .yoast-issue-counter, table.wp-list-table.plugins .notice-warning { display: none !important; }</style>';
    }
}   
add_action('admin_enqueue_scripts', 'hide_wp_dashboard_notices');
add_action('login_enqueue_scripts', 'hide_wp_dashboard_notices');
Share Improve this question edited Nov 28, 2018 at 16:25 Travis Pflanz asked Nov 27, 2018 at 18:26 Travis PflanzTravis Pflanz 1,9535 gold badges31 silver badges57 bronze badges 6
  • 2 What do you think about creating custom capability and applying it to selected users? developer.wordpress/plugins/users/roles-and-capabilities I can show you example if you would like this solution – Anton Lukin Commented Nov 27, 2018 at 18:55
  • @AntonLukin, I was thinking that was probably the best way to go, but I also figured that starting simple was the easiest way to start, with my limited dev skills. The flip side, I imagine, with using capabilities is that I would also need to be able to restrict other admins from adding/removing capabilities from users, because that would defeat the purpose of the plugin to hide the notifications, if they could just go turn them back on. – Travis Pflanz Commented Nov 27, 2018 at 19:32
  • Note that another admin can just upload custom plugin and turn on everything he wants (Include ignoring your mu-plugin functionality). So if you really want to restrict some features for another users, you should create new roles with custom capabilities. Or just ignore it – Anton Lukin Commented Nov 27, 2018 at 19:42
  • @AntonLukin - I understand that, but I don't want it to be simple for someone to just toggle it on and off, of course. I am interested in an example, as you mentioned in your initial comment. Thank you! – Travis Pflanz Commented Nov 27, 2018 at 21:04
  • 1 I will do soon. Have no time, sorry. – Anton Lukin Commented Dec 3, 2018 at 17:00
 |  Show 1 more comment

2 Answers 2

Reset to default 1

Your question boils down to simple PHP. It is basically how to avoid

'AdminUser' !== $user->user_login || 'AdminUser2' !== $user->user_login || 'AdminUser3' !== $user->user_login || 'AdminUser4' !== $user->user_login || etc.

One way to solve this is use in_array() instead:

$allowed_users = [
    'AdminUser',
    'AdminUser2',
    'AdminUser3',
    // etc
];
$user = wp_get_current_user();

if($user && isset($user->user_login) && !in_array($user->user_login, $allowed_users)) {
    echo '<style>.update-nag, .updated, .error, .is-dismissible, .notice.woo-permalink-manager-banner, #try-gutenberg-panel, span.update-plugins, .yoast-issue-counter, table.wp-list-table.plugins .notice-warning { display: none !important; }</style>';
}

I advise you to use custom capability as it is more correct WordPress way.

function wpse_320373_add_caps() {
    $allowed_users = ['admin', 'test'];

    // We add custom capability to each of allowed users after switch theme
    foreach ($allowed_users as $user_name) {
        $user = new WP_User('', $user_name);
        $user->add_cap( 'allow_notices' );
    }
}

add_action( 'after_switch_theme', 'wpse_320373_add_caps' );


function wpse_320373_notices() {
    $user = wp_get_current_user();

    // Check if the capability is set to current user
    if ( $user && !$user->has_cap( 'allow_notices' ) ) {
        echo '<style>.update-nag, .updated, .error, .is-dismissible, .notice.woo-permalink-manager-banner, #try-gutenberg-panel, span.update-plugins, .yoast-issue-counter, table.wp-list-table.plugins .notice-warning { display: none !important; }</style>';
    }
}
add_action('admin_enqueue_scripts', 'wpse_320373_notices');
add_action('login_enqueue_scripts', 'wpse_320373_notices');

Add this code to functions.php, replace allowed_users logins array and and re-activate your theme to apply new settings.

Note that capability setting is saved to the database, so it will be better to run this on theme/plugin activation but not every page load.

Post a comment

comment list (0)

  1. No comments so far