【问题标题】:how to restrict pages to a specified users based on their roles in wordpress如何根据他们在wordpress中的角色将页面限制为指定用户
【发布时间】:2018-07-31 17:14:54
【问题描述】:

我是 wordpress 新手,我有一个网站,其中包含一些与 wordpress 的基本角色不同的角色(管理员、作者、贡献者...)

我创建了具有以下能力的员工 chef_dept 角色:

add_role('Chef_Dept', __(
    'Chef_Dept'),
     array(
        'read'              => true, // Allows a user to read
        'create_posts'      => true, // Allows user to create new posts
        'edit_posts'        => true, // Allows user to edit their own posts
        'edit_others_posts' => true, // Allows user to edit others posts too
        'publish_posts'     => true, // Allows the user to publish posts
        'manage_categories' => true, // Allows user to manage post categories
        'upload_files'      => true, //  Allows user to upload file 
    )
);

add_role('employee ', __(
   'employee '),
    array(
        'read'              => true, // Allows a user to read
        'create_posts'      => true, // Allows user to create new posts
        'publish_posts'     => true, // Allows the user to publish posts
        'manage_categories' => true, // Allows user to manage post categories
    )
);


add_role( $role, $display_name, $capabilities );

我需要限制所有具有员工角色的用户不能进入某些页面,这可能吗?

我正在使用 Restrict Page By Role 插件,但它似乎不起作用,因为具有 employee 角色的用户能够上传文件并进入受限页面

【问题讨论】:

    标签: php wordpress user-roles


    【解决方案1】:

    你可以在你的functions.php中实现函数来解决这个问题,但是你必须为每个页面分配用户必须拥有的东西。

    function is_corr_user($page_slug) {
    
      // User has to be logged in
      if(!is_user_logged_in())
        return false;
    
      // All user roles
      $roles = wp_get_current_user()->roles;
    
      // For each page check if user has required role
      switch($page_slug) {
        case "page-1":
         return in_array('Chef_Dept', $roles);
        case "page-2":
         return in_array('Chef_Dept', $roles);
        default:
          return false:
      }
    }
    
    
    // Hook to wordpress before load and check if correct user is on page
    add_action( 'wp', 'wpse69369_is_correct_user' );
    function wpse69369_is_correct_user()
    {
        global $post;
    
        // Not working on homepage
        // Redirect to homepage if wrong user
        if(!is_home() && !is_correct_user($post->post_name)) {
          wp_redirect( '/' );
          exit;
        }     
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用插件:

      https://fr.wordpress.org/plugins/wp-jv-post-reading-groups/

      我在 Intranet 上使用它,因为它允许将 wordpress 的角色与公司的角色分离

      当您创建页面或文章时,您有一个选择框,可让您定义哪个“阅读组”可以访问它

      此外,它的创建者在出现问题时反应非常迅速

      【讨论】:

        【解决方案3】:

        // 示例1:限制访问整个站点并重定向到wordpress登录页面 add_action('template_redirect', function() {

        // Front end only and prevent redirections on ajax functions
        if ( is_admin() || wp_doing_ajax() ) {
            return;
        }
        
        // Redirect all pages to the login page if user isn't logged in
        if ( ! is_user_logged_in() ) {
            wp_redirect( esc_url( wp_login_url() ), 307 );
        }
        

        });

        // 示例2:限制对特定页面的访问 add_action('template_redirect', function() {

        // Get global post
        global $post;
        
        // Prevent access to page with ID of 2 and all children of this page
        $page_id = 170;
        if ( is_page() && ( $post->post_parent == $page_id || is_page( $page_id ) ) ) {
        
            // Set redirect to true by default
            $redirect = true;
        
            // If logged in do not redirect
            // You can/should place additional checks here based on user roles or user meta
            if ( is_user_logged_in() ) {
                $redirect = false;
            }
        
            // Redirect people without access to login page
            if ( $redirect ) {
                wp_redirect( esc_url( wp_login_url() ), 307 );
            }
        
        
        }
        

        });

        【讨论】:

          猜你喜欢
          • 2012-09-14
          • 1970-01-01
          • 1970-01-01
          • 2018-01-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-23
          • 2020-03-01
          相关资源
          最近更新 更多