latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Show content for one user

Home Forums Community Forum Show content for one user

This topic contains 2 replies, has 2 voices. Last updated by  David Welch 4 years, 10 months ago.

Topic Author Topic
Posted: Thursday Feb 16th, 2012 at 11:04 am #5369
Maryke
Username: maryke

I would like to show content for one user only. Username= annie

How do I do this?

List Of Topic Replies

Viewing 2 replies - 1 through 2 (of 2 total)
Author Replies
Author Replies
Posted: Thursday Feb 16th, 2012 at 11:43 am #5371
David Welch
Username: dwbiz05

Well, you could create a function that works like the level access shortcode for just one user.

However, if you haven’t used up all your membership levels (and don’t plan to) you could just use one for that one person, upgrade him/her to that level and then protect that content like normal.

The only issue is that member levels build on each other, meaning a level 4 member has access to all content that a level 3, 2, 1 or 0 member has. If that is a problem, then you probably will have to go the custom route.

I didn’t look throughly through the scripting/api options that s2member has, but I didn’t notice one that does this.

My quick idea is as follows:

function user_only_content($atts, $content=null){
	extract( shortcode_atts( array(
		'user_id' => 0,
	), $atts ) );
	
	if( is_user_logged_in() && S2MEMBER_CURRENT_USER_ID == $user_id){
		return $content;
	}
	
}

add_shortcode('user-specific-content','user_only_content');

Add that to your functions.php file in your theme.

Then to call it in your post or page, just use this:

[user-specific-content user_id="5"]
Place the content you want for user_id 5 between these tags and enter the user's id in the user_id="" in the first tag.
[/user-specific-content]

If you want to use the user’s login name instead of their ID, change this in the function:

S2MEMBER_CURRENT_USER_ID

to 

S2MEMBER_CURRENT_USER_LOGIN

Then put the username/login name in the user_id=”” part of the shortcode.

Hope that helps,

Dave

Posted: Thursday Feb 16th, 2012 at 12:44 pm #5382
David Welch
Username: dwbiz05

I just saw your other post about this and it looks like you want to use shortcode inside your shorcode for this function.

If so, you can change my function as follows and it should work to have shortcode in your text.

if( is_user_logged_in() && S2MEMBER_CURRENT_USER_LOGIN == $user_id){
		$content = preg_replace('/(\[.*?\])/e',"do_shortcode('$1')", $content);
		
		return $content;
	}

Dave

Viewing 2 replies - 1 through 2 (of 2 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.