如何删除作者的仪表板访问权限(wp-admin),但不禁用这些功能?

时间:2017-01-23 作者:Hend Ry

我希望作者角色不能访问wp admin,但仍然启用这些功能,以便他们可以从前端页面删除帖子?

2 个回复
最合适的回答,由SO网友:mukto90 整理而成

在您的functions.php 文件或插件中-

function wpse_253580_prevent_author_access(){
    if( current_user_can( \'author\' ) && is_admin() )  {
        // do something here. maybe redirect to homepage
        wp_safe_redirect( get_bloginfo( \'url\' ) );
    }
}
add_action( \'admin_init\', \'wpse_253580_prevent_author_access\' );
这将检查当前用户是否是author 他正试图进入wp-admin 地区如果为true,则将其重定向到homepage.

SO网友:Elly Post

Mukto90的上述答案有点过时。更好的方法是:

检查功能而不是角色名称(current_user_can 不支持角色名称,但不能保证其工作,并且可以重命名这些角色)

  • 仅在不执行AJAX时重定向,安全重定向后退出更新后的答案如下:

    add_action( \'admin_init\', function(){
        if( (!current_user_can( \'edit_others_posts\' ) && current_user_can(\'edit_posts\') && is_admin()) && !wp_doing_ajax() )  {
            // do something here. maybe redirect to homepage
            wp_safe_redirect( get_site_url() );
            exit;
        }
    } );