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');
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');
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>';
}
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.