首页 > 解决方案 > 如何隐藏和禁用 Wordpress 管理菜单项

问题描述

我有两个 Wordpress 管理员帐户,但我需要为一个管理员隐藏某些管理员菜单项。我已经找到了如何做到这一点,但是如何禁用通过 url 直接访问页面?像这样的高级访问管理器

到目前为止我有..

function hide_menu() {

    $user = wp_get_current_user();
    if($user && isset($user->user_login) && 'admin2' == $user->user_login) {
        
        remove_menu_page( 'plugins.php' ); //Plugins
        remove_menu_page( 'tools.php' ); //Tools
        remove_menu_page( 'users.php' ); //Users
        
    }
    
}

add_action('admin_head', 'hide_menu');

标签: wordpress

解决方案


简单的解决方案: 您可以使用此插件: https ://wordpress.org/plugins/prevent-direct-access/

编码解决方案:

    add_action('template_redirect', function() {
        // ID of the page you want to restrict from
        if (!is_page(2072)) {
           return;
        }

        // Provide the link of admin dashboard, so that page should only 
       // accessible 
      //  through admin dashboard
        if (wp_get_referer() === 'https://example.com/wp-admin/') {
            return;
        }

       // we are on restricted page
       // visitor is trying to access directly
        // so redirect to home
       wp_redirect(get_home_url());
        exit;
    } );

推荐阅读