Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 ?

share|improve this question
    
what was your solution? i am looking For something similar. In my case behind a restricted website a verified user has to use a multipage form which collects all the necessary info and after all the data has been collected the data info is sent to the admin –  alex Sep 9 at 4:50

3 Answers 3

up vote 6 down vote accepted

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;
}
?>
share|improve this answer
    
This is just what I was looking for! Just wanted so give some big thanks man! –  Cris Dec 30 '11 at 10:47

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);
share|improve this answer
    
only problem here is that it it's not really intuitive for the site owner to set 'permissions' per post –  hannit cohen Apr 10 '11 at 21:46
    
then checkout the "Members" plugin: justintadlock.com/archives/2009/09/17/members-wordpress-plugin it's much more user-friendly... –  onetrickpony Apr 10 '11 at 21:48
    
was thinking about this one, only I saw it wasn't updated in a while now. any idea if it still works ? –  hannit cohen Apr 10 '11 at 22:34
    
it throws up a few notices with wp_debug, but it works. I'm using it to control roles and caps –  onetrickpony Apr 11 '11 at 0:14

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 :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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