Blog / WordPress/ How to Hide WordPress Update Notices for Non-Admin Users

How to Hide WordPress Update Notices for Non-Admin Users

WordPress 隐藏普通用户的更新通知

Hide WordPress Update Notices for Non-Admin Users

By default, all users in the WordPress admin area see update notifications for the core, themes, and plugins. On multi-user sites, you may want to restrict these notices to administrators (or users with specific capabilities) to prevent confusion or unnecessary actions by regular users.

Implementation

Add the following code to your theme's functions.php file or a custom functionality plugin:

/**
 * Hide WordPress update notices for non-admin users.
 */
function hide_update_notice_for_non_admins() {
    if ( ! current_user_can( 'update_core' ) ) {
        remove_action( 'admin_notices', 'update_nag', 3 );
    }
}
add_action( 'admin_head', 'hide_update_notice_for_non_admins', 1 );

How It Works

  • Function Logic: The function checks if the current user has the update_core capability (a core capability for administrators). If not, it uses remove_action to detach the update_nag hook that triggers the main update notice.
  • Hook Choice: The admin_head action hook ensures the check runs early in the admin header, removing the notice before it is displayed.
  • Capability Check: Using current_user_can('update_core') is a standard, flexible way to determine if a user has update privileges, preferable to checking for a specific role.

Important Notes

  • This method only hides the primary core update notice. Notifications for plugin or theme updates may be triggered by other hooks and require separate handling.
  • To allow other roles (e.g., Editors) to see updates, modify the capability check—for example, use current_user_can('manage_options') or a custom capability.
  • Before editing functions.php, consider using a child theme or backing up the file.

Tip: For finer control (e.g., hiding specific plugin/theme update notices), identify and remove the corresponding hooks. Development plugins like "Query Monitor" can help list all action hooks on an admin page.

Post a Comment

Your email will not be published. Required fields are marked with *.