0

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');
6
  • 2
    What do you think about creating custom capability and applying it to selected users? developer.wordpress.org/plugins/users/roles-and-capabilities I can show you example if you would like this solution 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. 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 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! Commented Nov 27, 2018 at 21:04
  • 1
    I will do soon. Have no time, sorry. Commented Dec 3, 2018 at 17:00

2 Answers 2

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>';
}
4
  • This is an improvement but still fundamentally the same approach
    – Tom J Nowell
    Commented Nov 27, 2018 at 18:38
  • 1
    @TomJNowell Yes, but it answers the original question (adding multiple users) and avoids chains of OR. Did I miss a part in the question?
    – kero
    Commented Nov 27, 2018 at 18:44
  • I assumed it was that they wanted a general solution that matched against the administrator role rather than usernames
    – Tom J Nowell
    Commented Nov 27, 2018 at 21:36
  • I went ahead with this method, though I do know it's not 100% the right way to completely accomplish what I am doing. Much appreciated. Thanks! Commented Nov 29, 2018 at 16:23
0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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