Skip to content

Instantly share code, notes, and snippets.

@ghwoodard
Created February 1, 2024 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghwoodard/6b63e97b457c542e2d458b335eaa6c60 to your computer and use it in GitHub Desktop.
Save ghwoodard/6b63e97b457c542e2d458b335eaa6c60 to your computer and use it in GitHub Desktop.
<?php
// Hide Admin Notices
function hide_update_msg_non_admins(){
if (current_user_can( 'manage_options' )) { // User Caps
echo '<style>#admin-notice { display: none; } .e-notice--dismissible { display: none; }</style>';
}
}
add_action( 'admin_head', 'hide_update_msg_non_admins');
// Alternate Version - More PHP Focused
function dsourc_hide_notices(){
$user = wp_get_current_user();
if (!($user->roles[0] == 'administrator')) {
remove_all_actions( 'admin_notices' );
}
}
add_action( 'admin_head', 'dsourc_hide_notices', 1 );
@ghwoodard
Copy link
Author

There are some edits to make these customizable to your needs:

  • Change/Update line 5 for the user role cap you want to affect
  • Change/Update line 6 to reflect the CSS selector/ID of the notices

Alternate Version

  1. Line 12: Create a Function name as you want.
  2. Line 13: fetch the Current user, whether he is an admin or on your custom role user,
  3. Line 14: Detect your admin pages by using “if (!($user->roles[0] == ‘administrator’))” You need to know when to remove the notices and limit that action only to your administration pages. Hence, you need a way to determine when the user is looking at your pages, for example, looking at the “page” request parameter.
  4. Line 15: Remove all registered admin notice actions WP lets everyone to remove all attached functions to an event, so we can remove all other plugins/theme actions.
  5. Line 18: Add action to remove notices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment