我在WordPress开发中进行了搜索,但找不到答案,所以我要么搜索得不好,要么找不出需要搜索的实际术语。。。
我正在尝试创建一个简单的mu插件来删除更新通知、NAG和其他随机通知,这些通知是由我在许多网站上为客户端使用的许多插件创建的。该插件将删除除单个用户之外的所有用户的通知。
下面的代码可以工作,但我知道我没有删除核心、插件和主题通知。
也就是说,我现在试图解决的问题(没有用)是能够添加多个能够看到通知的用户(通过用户名)。。。所以AdminUser 和AdminUser2 和AdminUser3 应该在登录时看到通知。
我不是一个开发人员,所以非常感谢您的帮助。
//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\');
最合适的回答,由SO网友:kero 整理而成
您的问题归结为简单的PHP。基本上就是如何避免
\'AdminUser\' !== $user->user_login || \'AdminUser2\' !== $user->user_login || \'AdminUser3\' !== $user->user_login || \'AdminUser4\' !== $user->user_login || etc.
解决此问题的一种方法是使用
in_array() 而是:
$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>\';
}
SO网友:Anton Lukin
我建议您使用自定义功能,因为这是更正确的WordPress方式。
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\');
将此代码添加到函数。php,替换
allowed_users 登录阵列并重新激活主题以应用新设置。
请注意,功能设置保存到数据库中,因此最好在主题/插件激活时运行此设置,而不是在每次加载页面时运行。