• Can anyone please help me with this…

    I want to choose which users can see certain posts.

    I saw a plugin for doing this based on user roles but that won’t work because all users have to be the same role (subscribers) so I don’t want to change that.

    For example… when I make a post, I only want bob3112, john45, and mike77 to be able to read the post… but I DON’T want steve11, troy334, and others to see it…

    I know I can password protect the post but I don’t want to contact each of the users with the new password every time…

    I just want to be able to select which users can or cannot read the post.

    SOMEONE PLEASE HELP!! I’ve been searching high and low for a really long time… Any help will be appreciated.

    Thanks,
    Anthony

Viewing 7 replies - 1 through 7 (of 7 total)
  • You could add a shortcode to your theme so that you can wrap blog content you want to hide to be visible to certain users only. An example is as follows:

    <?php
    
    function members_shortcode($atts, $content=null ){
    
    	global $current_user;
    	get_currentuserinfo();
    
    	$current_user = $current_user->user_login;
    	$allowed = array( 'bob3112', 'john45', 'mike77' );
    
    	if ( is_user_logged_in() && !is_null( $content ) && !is_feed() && in_array( $current_user, $allowed ) )
    		return $content;
    	return '';
    }
    add_shortcode( 'specific_members', 'members_shortcode' );
    
    ?>

    Please not this code is untested, but will point you in the right direction at least.

    Example usage in your post editor would then be:

    [specific_members]Add post content here that you only want certain users to see.[/specific_members]

    If list of users was not constant for all posts then you could extend this shortcode to accept the user list as a parameter, but you will have to figure that out for yourself! ??

    Thread Starter Tony

    (@acfurino)

    Thank you so much although I’m not sure exactly what to do because I’m still a fairly new user of wordpress but I will toy around with it… hopefully I don’t mess up anything…

    What do you mean in this last part and how would you suggest I figure that out?

    “If list of users was not constant for all posts then you could extend this shortcode to accept the user list as a parameter, but you will have to figure that out for yourself! ?? “

    All I meant was that if you wanted different users to be able to view certain posts you would need a way to pass the list of users to the shortcode, i.e. something like this:

    [specific_members users="bob3112', 'john45', 'mike77"]
    Add post content here that you only want certain users to see.
    [/specific_members]

    But that would need some more code added, I would do it for you but I just don’t have the time right now. You can see here how to add shortcode parameters but it may be over your head if you’re new to WordPress coding:

    https://codex.www.ads-software.com/Shortcode_API

    To test the code I showed in my original post just go to Appearance -> Editor and edit your functions.php file (good idea to back it up first), and add this code before the closing php bracket.

    function members_shortcode($atts, $content=null ){
    
    	global $current_user;
    	get_currentuserinfo();
    
    	$current_user = $current_user->user_login;
    	$allowed = array( 'bob3112', 'john45', 'mike77' );
    
    	if ( is_user_logged_in() && !is_null( $content ) && !is_feed() && in_array( $current_user, $allowed ) )
    		return $content;
    	return '';
    }
    add_shortcode( 'specific_members', 'members_shortcode' );

    Add valid usernames to the array specified in:

    $allowed = array( 'bob3112', 'john45', 'mike77' );

    Then in a blog post add:

    [specific_members]Add post content here that you only want certain users to see.[/specific_members]

    Try, viewing the blog post as a guest, or as a user who is not in the list, and a user(s) who ARE in the list see what results you get.

    Let me know how you get on.

    Thread Starter Tony

    (@acfurino)

    I think that’s what I’m looking for…

    [specific_members users=”bob3112′, ‘john45’, ‘mike77″]
    Add post content here that you only want certain users to see.
    [/specific_members]

    …because I’m going to be making different posts on a daily basis and I want different users to be able to view each of them (not all the same).

    Example:
    Post 1 can be viewed by bob3112 and john45 but not mike77
    Post 2 can be viewed by mike77 and bob3112 but not john45
    etc.

    I appreciate all of the info that you’ve given me thus far and I respect the fact that you don’t have the time right now… Do you have a business or website you can refer me to?

    I meant I don’t have the time to do it for free. ??

    However, you are welcome to contact me via my site for a quote if you wish. Just click on my user profile for a link to my WordPress development site.

    Thread Starter Tony

    (@acfurino)

    I sent in a contact form a couple of hours ago but I’m researching it now… I may be able to figure it out without you but try to reply asap if you still want to help.

    Thread Starter Tony

    (@acfurino)

    Does anyone else know how to do this? Obviously, I’d rather get some free help rather than having to spend money.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Only select users can view certain posts’ is closed to new replies.