How do I display a user specific content?

I have a site that displays the content only to registered users.
Most of the content is available to all the users, but some of it is user specific (i.e. all users have that page, but each sees a it’s own content which no one else can see).
Any idea as to how I can restrict(and display) specific content per specific user ?

Related posts

Leave a Reply

4 comments

  1. I had coded something like this a while back and i remember wanting to upload it to the plugin repository but never had the time, Basically it adds a meta box to the post or page edit screen and lets the user select specific users by name or roles and then it check using the_content filter, so here you go:

    Update:

    It just got approved in to WordPress plugin repository so you can download it User specific content form there or from your dashboard and i wrote a little about it here.

    enter image description here

    <?php
    /*
    Plugin Name: User Specific Content
    Plugin URI: http://en.bainternet.info
    Description: This Plugin allows you to select specific users by user name, or by role name who can view a  specific post content or page content.
    Version: 0.1
    Author: Bainternet
    Author URI: http://en.bainternet.info
    */
    
    /* Define the custom box */
    add_action('add_meta_boxes', 'User_specific_content_box');
    
    /* Adds a box to the main column on the custom post type edit screens */
    function User_specific_content_box() {
        add_meta_box('User_specific_content', __( 'User specific content box'),'User_specific_content_box_inner','post');
        add_meta_box('User_specific_content', __( 'User specific content box'),'User_specific_content_box_inner','post');
    }
    
    /* Prints the box content */
    function User_specific_content_box_inner() {
        global $post,$wp_roles;
        $savedroles = get_post_meta($post->ID, 'U_S_C_roles',true);
        //var_dump($savedroles);
        $savedusers = get_post_meta($post->ID, 'U_S_C_users',true);
        //var_dump($savedusers);
        // Use nonce for verification
        wp_nonce_field( plugin_basename(__FILE__), 'User_specific_content_box_inner' );
        echo __('Select users to show this content to');
        echo '<h4>'.__('By User Role:').'</h4>';
        if ( !isset( $wp_roles ) )
            $wp_roles = new WP_Roles();
        foreach ( $wp_roles->role_names as $role => $name ) {
            echo '<input type="checkbox" name="U_S_C_roles[]" value="'.$name.'"';
            if (in_array($name,$savedroles)){
                echo ' checked';
            }
            echo '>'.$name.'    ';
        }
        echo '<h4>'.__('By User Name:').'</h4>';
        $blogusers = get_users('blog_id=1&orderby=nicename');
        $usercount = 0;
        foreach ($blogusers as $user) {
            echo '<input type="checkbox" name="U_S_C_users[]" value="'.$user->ID.'"';
            if (in_array($user->ID,$savedusers)){
                echo ' checked';
            }
            echo '>'.$user->display_name.'    ';
            $usercount = $usercount + 1;
            if ($usercount > 5){
                echo '<br/>';
                $usercount = 0;
            }
        }
        echo '<h4>'.__('Content Blocked message:').'</h4>';
        echo '<textarea rows="3" cols="70" name="U_S_C_message" id="U_S_C_message">'.get_post_meta($post->ID, 'U_S_C_message',true).'</textarea><br/>'.__('This message will be shown to anyone who is not on the list above.');
    }
    
    
    /* Save Meta Box */
    add_action('save_post', 'User_specific_content_box_inner_save');
    
    /* When the post is saved, saves our custom data */
    function User_specific_content_box_inner_save( $post_id ) {
        global $post;
          // verify this came from the our screen and with proper authorization,
          // because save_post can be triggered at other times
    
          if ( !wp_verify_nonce( $_POST['User_specific_content_box_inner'], plugin_basename(__FILE__) ) )
              return $post_id;
    
          // verify if this is an auto save routine. 
          // If it is our form has not been submitted, so we dont want to do anything
          if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
              return $post_id;
          // OK, we're authenticated: we need to find and save the data
        $savedroles = get_post_meta($post_id, 'U_S_C_roles',true);
        $savedusers = get_post_meta($post_id, 'U_S_C_users',true);
        if (isset($_POST['U_S_C_roles']) && !empty($_POST['U_S_C_roles'] )){
            foreach ($_POST['U_S_C_roles'] as $role){
                $new_roles[] = $role;
            }
            update_post_meta($post_id, 'U_S_C_roles', $new_roles);
        }else{
            if (count($savedroles) > 0){
                 delete_post_meta($post_id, 'U_S_C_roles');
            }
        }
        if (isset($_POST['U_S_C_users']) && !empty($_POST['U_S_C_users'])){
            foreach ($_POST['U_S_C_users'] as $u){
                $new_users[] = $u;
            }
            update_post_meta($post_id, 'U_S_C_users', $new_users);
        }else{
            if (count($savedusers) > 0){
                 delete_post_meta($post_id, 'U_S_C_users');
            }
        }
        if (isset($_POST['U_S_C_message'])){
            update_post_meta($post_id,'U_S_C_message', $_POST['U_S_C_message']);
        }
    }
    
    add_filter('the_content','User_specific_content_filter');
    function User_specific_content_filter($content){
        global $post,$current_user;
    
        $savedroles = get_post_meta($post->ID, 'U_S_C_roles',true);
        $run_check = 0;
        $savedusers = get_post_meta($post->ID, 'U_S_C_users',true);
        if (!count($savedusers) > 0 && !count($savedroles) > 0 )
            return $content;
    
        if (isset($savedroles) && !empty($savedroles)){
            foreach ($savedroles as $role){
                if (current_user_can($role)) {
                    return $content;
                    exit;
                }
            }
            //failed role check
            $run_check = 1;
        }
        if (isset($savedusers) && !empty($savedusers)){
            get_currentuserinfo();
            if (in_array($current_user->ID,$savedusers)){
                return $content;
            }
                //failed both checks
            return get_post_meta($post->ID, 'U_S_C_message',true);
        }
        return $content;
    }
    ?>
    
  2. Assuming this content is the usual post loop:

    $current_user = wp_get_current_user();
    if(get_the_author_meta('ID') === $current_user->ID):
    
      // show the content
    
    endif;
    

    I think this only works inside the loop.

    If you need it outside the loop then just query posts from $current_user->ID:

    $query = new WP_Query('author' => $current_user->ID);
    
  3. In addidtion to what One Trick Pony wrote, if the site owner needs the ability to restrict the content to each user, you can develop a small custom meta box that will appear inside each post and will display checkboxes with the site’s users and then you will have in your dayabse the needed post_meta of the users that have permission to the content and you could make the condition.
    If you need another way of controlling the content, it’s better you provide some more specific details on how this should and needed to be handled, so it will be easier to think for appropriate solutions…

    Good luck 🙂

  4. 1st approach:

    1. One method of doing this to create individual page for each student and make this page as password protected. That password can be given to student.
    2. You can allow comment for that Page/Post so that student can add there queries and responses.

    2nd approach:

    1. You can use WP Private Content Plus (https://wordpress.org/plugins/wp-private-content-plus/) and make each page/post visible to specif users only.
    2. You can create any types of user menu of admin.

    You can also use 2nd approach with 1st for additional security though its optional.