0

I am designing a site in which I wish to have different users (logged in) for different pages.

Illustration:

Suppose I have subscribers A,B,C,D,E,F and pages 1,2 and 3.

I wish to have the follwing functionality:

A can access pages 1,2 and not 3 B can access pages 1 and not 2 and 3 C can access all pages.

I tried using the following plugins:

Capability Manager Contexture page security members User access manager user groups page restrict (This can restrict the access of anonymous to certain pages and only logged in users can view locked pages.) But I'm not able get the functionality I require even after using any combinations of above plugins.

Require some help, I'm a newbie.

Thanks in advance.

2
  • This - again - might be easier with a multisite/network install. Simply add different users to different sites.
    – kaiser
    Sep 2, 2013 at 11:36
  • Thank you for your reply. Is ther any other way of achieving this?
    – rockswap
    Sep 2, 2013 at 11:59

1 Answer 1

0
  1. Create a subfolder in plugins folder, name it 'MyRestict'. In This folder put the following php:

    <?php 
    /**
     * Plugin Name: My Restict
     * Plugin URI: http://wordpress.stackexchange.com/questions/112566/how-to-set-different-users-for-different-pages
     * Author: G.M.
     */
    
    function my_restict_template_filter( $template ) {
      if ( is_page() ) {
        $post = get_queried_object();
        $allowed = (string) get_post_meta($post->ID, 'allowed_users', true);
        if ( $allowed ) $allowed = array_map('trim', explode(',', $allowed) );
        $user = wp_get_current_user();
        if ( ! empty($allowed) && ( ! is_object($user) || ! in_array($user->ID, $allowed) ) )
          return plugin_dir_path(__FILE__) . 'not-allowed.php';
      }
      return $template;
    }
    
    add_filter('template_include', 'my_restict_template_filter');
    
  2. In the same 'MyRestict' folder create a file named not-allowed.php and there put something like:

    <div>Sorry, you are not allowed to view this page.</div>
    
  3. In your pages you have to create a custom field with the key allowed_users where you have to write comma separed list of users ids of users that you want to allow viewing that page: enter image description here

  4. Activate the 'My Restict' plugin in the backend

Remember that pages that do not have any custom field will be accessible to all visitors, even not logged ones.

4
  • Thank you for answering the question. I tried doing what you mentione but the above plugin does not produce any effect.
    – rockswap
    Sep 2, 2013 at 14:56
  • @rockswap there was a little error on my code. I've corrected, now it should works.
    – gmazzap
    Sep 2, 2013 at 15:10
  • That worked amazingly. Thank you. :) Sorry, I dont have enough reputation to vote up. Once I have I'll vote up.
    – rockswap
    Sep 2, 2013 at 15:34
  • @rockswap glad it works.
    – gmazzap
    Sep 2, 2013 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.