12

This question already has an answer here:

I'm trying to remove or hide the update nags for non-admin users. As an admin, I see:

enter image description here

The popular answer I've seen to handle this says to use:

function hide_update_nag() {
    if ( !current_user_can('update_core') ) {
        remove_action( 'admin_notices', 'update_nag', 3 );
    }
}
add_action( 'admin_head', 'hide_update_nag', 1 );

This works fine for removing the first message (WordPress 4.5.3 is available! Please update now) but leaves the second one visible to non-admins:

enter image description here

Both messages are wrapped in a <div class="update-nag">, so one option is to modify the above chunk of code to use CSS to hide the nag with:

echo '<style>.update-nag {display: none}</style>';

But this feels kludgy to me. Is there a way to hook into an action or filter and remove ALL the update nag messages for non-admin users? No third-party plugin recommendations please.

marked as duplicate by Nathan Johnson, EAMann Mar 3 '18 at 0:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • check this – bravokeyl Jun 29 '16 at 14:13
  • @bravokeyl - I actually linked to that answer in my question. – j08691 Jun 29 '16 at 14:17
  • Oops! my bad.I didn't check. – bravokeyl Jun 29 '16 at 14:19
  • @bravokeyl - The admin_notices only removes the first of the two messages and it seems that removing ( or hiding) the second message requires CSS. – j08691 Jun 29 '16 at 14:21
16

In wp-admin/includes/update.php file

if ( current_user_can('update_core') )
        $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
    else
        $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');

We can see that messages are different based on the current user role and this is maintenance_nag.

Basically we have two update nags and can be found in admin-filters.php

add_action( 'admin_notices', 'update_nag',      3  );
add_action( 'admin_notices', 'maintenance_nag', 10 );

So to remove second message we can use(also check for current user role if you want this only for non-admins)

remove_action( 'admin_notices', 'maintenance_nag', 10 );

For multi-site use

remove_action( 'network_admin_notices', 'maintenance_nag', 10 );
2

here is complete code, which seems to work at this moment:

add_action('admin_head', function() {
    if(!current_user_can('manage_options')){
        remove_action( 'admin_notices', 'update_nag',      3  );
        remove_action( 'admin_notices', 'maintenance_nag', 10 );
    }
});
1

@bravokeyl is the probably the best answer to your immediate problem.

But to address the following:

Is there a way to hook into an action or filter and remove ALL the update nag messages for non-admin users?

No. Nag messages in WordPress are just a callback to added to the admin_notices hook which print some HTML to the page. They are practically the same as error or success messages, or any other 'notice' from WordPress or any other plug-in or theme for that matter.

Hiding the nags via CSS is hacky. It's also liable to some false positives as some plugins/themes will, incorrectly, use the .update-nag class to provide the desired styling to their own notices.

A much less hacky way is to explicitly remove each callback that you don't want printing notices (for non-admins). But this comes at a (probably very low cost) of maintaining that list and ensuring there are no notices that 'slip the net'.

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