How to hide admin notices for a specific role in WordPress admin
WordPress notifications and warnings are useful for us to update us on the status of our website. It will let us know if we did any bad configurations or any mistake which we need to fix. Now it is also irritating us sometime. Let us suppose we are the administrator of our website and we have a functionality like few of our customers come to the website and register themselves. They can update their profile from the WordPress admin and we can show their data at any place at front-end.
So, when they will login and move to the admin dashboard then they are watching our misconfiguration notices etc which is not good for customer relationship.
In this case we don’t want to show the notices to specific user role like our customers but we want to show it for the website owner. Generally website owners are the administrators and
customers having any role. Now we have a need to hide these notices to specific type of user role and we have two approaches to deal with this situation.
First Approach :
Hide all admin notices for a user role “subscriber”. Just need to go to our functions.php file of current theme and paste the below mentioned code:
add_action('admin_head','admin_css'); //using action hook to modify css function admin_css() { if(current_user_can('subscriber')) //check the current user role { echo '<style>'; echo '.notice{display:none}'; echo '</style>'; } }
Look at the above code, we hide the notices for a user role “subscriber”. You can change it for any type of user role.
We have observed that generally people want to hide notices for all type of user roles except “administrator”. In that case use the below mentioned code :
add_action('admin_head','admin_css'); //using action hook to modify css function admin_css() { if(!current_user_can('administrator')) //check the current user role which is not admin { echo '<style>'; echo '.notice{display:none}'; echo '</style>'; } }
Second Approach :
This approach includes a plugin “User role Editor”. This plugin is managing almost all options to set the new capabilities for all types of user roles. But it is missing the option to hide notices for a individual role. So, we have extend the functionality of that plugin so that it will also have option to hide admin notices.
To do this just install this plugin “User role Editor” and set the capabilities of user role as per your choice. Now, just open your functions.php file and paste the below mentioned code :
/* added this code to add a new option to user role editor plugin admin option under additional options */ function ure_add_block_admin_notices_option($items) { $item = URE_Role_Additional_Options::create_item('block_admin_notices', esc_html__('Hide admin notices', 'user-role-editor'), 'admin_init', 'ure_block_admin_notices'); $items[$item->id] = $item; return $items; } function ure_block_admin_notices() { add_action('admin_print_scripts', 'ure_remove_admin_notices'); } function ure_remove_admin_notices() { global $wp_filter; if (is_user_admin()) { if (isset($wp_filter['user_admin_notices'])) { unset($wp_filter['user_admin_notices']); } } elseif (isset($wp_filter['admin_notices'])) { unset($wp_filter['admin_notices']); } if (isset($wp_filter['all_admin_notices'])) { unset($wp_filter['all_admin_notices']); } } add_filter('ure_role_additional_options', 'ure_add_block_admin_notices_option', 10, 1);
Above mentioned code will add a option to the plugin settings. Just look at the screen to set the capabilities for any particular role in admin. Scroll down the screen you will find a new option in “Additional Options” section to hide admin notices for that user role. Just check this for selected role and you will find all notices for your selected user role are not showing.
You can choose which approach suits you better, both will hide the admin notices for all the users having the same role of our choice 🙂
Thanks! i was looking for this, i hope this works