在u-plugin中添加OR,以检查多个用户中是否有一个是登录用户

时间:2018-11-27 作者:Travis Pflanz

我在WordPress开发中进行了搜索,但找不到答案,所以我要么搜索得不好,要么找不出需要搜索的实际术语。。。

我正在尝试创建一个简单的mu插件来删除更新通知、NAG和其他随机通知,这些通知是由我在许多网站上为客户端使用的许多插件创建的。该插件将删除除单个用户之外的所有用户的通知。

下面的代码可以工作,但我知道我没有删除核心、插件和主题通知。

也就是说,我现在试图解决的问题(没有用)是能够添加多个能够看到通知的用户(通过用户名)。。。所以AdminUserAdminUser2AdminUser3 应该在登录时看到通知。

我不是一个开发人员,所以非常感谢您的帮助。

//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\');

2 个回复
最合适的回答,由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 登录阵列并重新激活主题以应用新设置。

请注意,功能设置保存到数据库中,因此最好在主题/插件激活时运行此设置,而不是在每次加载页面时运行。

结束

相关推荐

Mu-plugins文件夹在多站点安装中是如何工作的?

我对什么时候应该将插件文件放入mu插件有点困惑。我知道它必须使用插件,而且我知道它们是自动启用的。但是,在网络设置中,这是否会强制在所有子博客中启用插件文件?这是有道理的,那么这是我唯一一次应该在网络中放置插件文件吗?我什么时候希望插件代码在每个子博客中运行?例如,如果我想自动设置一个新的缩略图大小,我应该在那里这样做吗?或者还有其他例子吗。那么网络激活呢?