【问题标题】:How would I hide WordPress pages from non-administrators in the Dashboard?如何在仪表板中对非管理员隐藏 WordPress 页面?
【发布时间】:2021-10-29 17:04:10
【问题描述】:

我需要向非管理员的任何人隐藏 WordPress 页面仪表板中的某些页面,我想限制我的客户编辑它们,因为这些是自动创建的页面(在主题激活时)并且仅适用于模板。

这篇文章here 展示了如何按页面/帖子“ID”隐藏页面,但我需要通过页面 slug 来完成,否则每次有人激活我的主题时,他们都必须四处寻找页面 ID输入到functions.php文件中。

此示例中的 3 个页面的名称或名称为 products、policy 和 services

下面的代码对我没有任何作用?

add_action('admin_head', 'hide_posts_pages');

function hide_posts_pages() {
    global $current_user;
    wp_get_current_user();
    If($current_user->user_login != 'admin') {
        ?>
        <style>
           slug ==> 'products', slug ==> 'policy', slug ==> 'services', {
                display:none;
           }
        </style>
        <?php
    }
}

~ 有什么建议吗? 提前致谢!

【问题讨论】:

    标签: php wordpress function wordpress-theming dashboard


    【解决方案1】:

    将以下函数放在您的 functions.php 中。在$hidden_slugs 添加你想隐藏的蛞蝓。我们需要页面 id 来过滤它们。使用get_page_by_path 有助于根据 slug 查找 id。

    具有能力的解决方案 1:

    add_filter('parse_query', 'hide_pages_by_slug_if_not_admin');
    function hide_pages_by_slug_if_not_admin($query) {
        if(!is_admin()) return;
        
        if( is_admin() && current_user_can('manage_options')) {
            return;
        } else {
            // Create array of all the slugs you wanna hide
            $hidden_slugs = array( 'products', 'policy','services');
    
            // Loop through slugs & pass each slug as page path value
            foreach ($hidden_slugs as $hidden ) {
    
                $hidden_slugs[] = get_page_by_path( $hidden )->ID;
    
            // In case you need to hide the home page too uncomment
            //$hidden_slugs[] += get_option('page_on_front');
    
            }
            $query->query_vars['post__not_in'] =  $hidden_slugs;
        }
    }
    

    具有用户角色的解决方案 2:

    add_filter('parse_query', 'hide_pages_by_slug_if_not_admin');
    function hide_pages_by_slug_if_not_admin($query) {
        if(!is_admin()) return;
    
        $user = wp_get_current_user(); // Current user
        $allowed_roles = array('administrator'); // Allowed roles
        
        if( is_admin() && array_intersect($allowed_roles, $user->roles ) ) {
            return;
        } else {
    
            // Create array of all the slugs you wanna hide
            $hidden_slugs = array( 'products', 'policy','services');
    
            // Loop through slugs & pass each slug as page path value
            foreach ($hidden_slugs as $hidden ) {
    
            $hidden_slugs[] = get_page_by_path( $hidden )->ID;
    
            // In case you need to hide the home page too uncomment
            // $hidden_slugs[] += get_option('page_on_front');
    
            }
            // Pass the array as value to the query vars filter 
            $query->query_vars['post__not_in'] =  $hidden_slugs;
        }
     }
    

    请记住,这不会阻止他们进行编辑。如果有权编辑页面的人只需输入 url wp-admin/post.php?post=postID&action=edit 仍然可以进行编辑。可以通过多种方式找到某个帖子的 ID。

    以下是一种快速解决方案,您可以使用它来防止编辑这些页面:

    // Prevent access to restricted pages
    if(isset($_GET['post'])) {
        $current_postID = $_GET['post'];
        if (in_array($current_postID, $hidden_slugs)) {
            $url=  admin_url().'edit.php?post_type=page';
            wp_redirect($url);
            exit;
        }
    }
    

    在$query-&gt;query_vars['post__not_in'] = $hidden_slugs;之后添加以下内容

    【讨论】:

    • current_user_can() 只能用于功能而非角色。您应该使用current_user_can('manage_options') 或实际检查$user-&gt;roles
    • @Martin Mirchev:非常感谢您的回答,非常感谢您为我写下所有内容,它隐藏了页面就好了!但是,当我尝试删除一个页面时,它会抛出一个错误并在此处抱怨这行代码:if (in_array($current_postID, $hidden_slugs)) { 参见屏幕截图:prnt.sc/1qw0xi9 以及此处:prnt.sc/1qw17k5,感谢您的启发!
    • @Howard E 你能详细说明一下代码 sn-p 吗?
    • 我的评论是针对答案的......他相应地更新了他的答案。
    • 我在删除页面时测试了代码并且没有收到任何错误。以下片段只是检查是否不允许用户编辑此页面以重定向他。注释 sn-p 并测试是否仍然出现错误。我还看到您的主题中名为 pagesetup.php 的文件中有错误。建议你把这个函数移到functions.php中测试一下。
    猜你喜欢
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 2013-12-17
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2020-05-24
    相关资源
    最近更新 更多