spot_img
HomeWordPress教程wordpress建站公司 完全删除特定用户角色的 WP_Admin_Bar

wordpress建站公司 完全删除特定用户角色的 WP_Admin_Bar

JasperAI 10000字免费额度试用

如何隐藏/删除具有特定角色的用户登录时显示的管理栏? 我想我必须做点什么 remove_menu(),但不完全是什么以及如何。

法典

将以下内容添加到您的 functions.php 此处详述的文件。

为所有人禁用管理栏:

// Disable Admin Bar for everyone
if (!function_exists('disable_admin_bar')) {

    function disable_admin_bar() {

        // for the admin page
        remove_action('admin_footer', 'wp_admin_bar_render', 1000);
        // for the front-end
        remove_action('wp_footer', 'wp_admin_bar_render', 1000);

        // css override for the admin page
        function remove_admin_bar_style_backend() { 
            echo '<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>';
        }     
        add_filter('admin_head','remove_admin_bar_style_backend');

        // css override for the frontend
        function remove_admin_bar_style_frontend() {
            echo '<style type="text/css" media="screen">
            html { margin-top: 0px !important; }
            * html body { margin-top: 0px !important; }
            </style>';
        }
        add_filter('wp_head','remove_admin_bar_style_frontend', 99);
    }
}
add_action('init','disable_admin_bar');

为除管理员以外的所有人禁用管理栏:

// Disable Admin Bar for everyone but administrators
if (!function_exists('disable_admin_bar')) {

    function disable_admin_bar() {

        if (!current_user_can('manage_options')) {

            // for the admin page
            remove_action('admin_footer', 'wp_admin_bar_render', 1000);
            // for the front-end
            remove_action('wp_footer', 'wp_admin_bar_render', 1000);

            // css override for the admin page
            function remove_admin_bar_style_backend() { 
                echo '<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>';
            }     
            add_filter('admin_head','remove_admin_bar_style_backend');

            // css override for the frontend
            function remove_admin_bar_style_frontend() {
                echo '<style type="text/css" media="screen">
                html { margin-top: 0px !important; }
                * html body { margin-top: 0px !important; }
                </style>';
            }
            add_filter('wp_head','remove_admin_bar_style_frontend', 99);

        }
    }
}
add_action('init','disable_admin_bar');

通过用户 ID 为特定用户禁用 WP 管理栏:

// Disable Admin Bar for specific user
if (!function_exists('disable_admin_bar')) {

    function disable_admin_bar() {

        // we're getting current user ID
        $user = get_current_user_id();

        // and removeing admin bar for user with ID 123
        if ($user == 123) {

            // for the admin page
            remove_action('admin_footer', 'wp_admin_bar_render', 1000);
            // for the front-end
            remove_action('wp_footer', 'wp_admin_bar_render', 1000);

            // css override for the admin page
            function remove_admin_bar_style_backend() { 
                echo '<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>';
            }     
            add_filter('admin_head','remove_admin_bar_style_backend');

            // css override for the frontend
            function remove_admin_bar_style_frontend() {
                echo '<style type="text/css" media="screen">
                html { margin-top: 0px !important; }
                * html body { margin-top: 0px !important; }
                </style>';
            }
            add_filter('wp_head','remove_admin_bar_style_frontend', 99);

        }
    }
}
add_action('init','disable_admin_bar');

这是一个 6 行代码块,它将删除非贡献者用户的管理栏:

高质量外链购买
add_action( 'init', 'fb_remove_admin_bar', 0 );
function fb_remove_admin_bar() {
    if (!current_user_can('edit_posts')) { // you can change the test here depending on what you want
        add_filter( 'show_admin_bar', '__return_false', PHP_INT_MAX );
    }
}

将其放入您的 function.php 文件或您的自定义插件中。

5分钟生成10篇英文软文article forge软件试用
siteground guangda
WordPress花园官方账号
WordPress花园隶致力于为广大跨境电商和独立站爱好者提供优质的WordPress教程、Woocommerce教程、Facebook、Twitter、tiktok、Instagram教程和谷歌SEO教程等资料和对应的建站推广服务。关注‘哟派出海’公众号了解最新资讯。粉丝福利:Shopline免费独立站建设14天优惠 商务合作: [email protected]
RELATED ARTICLES
spot_img