Control WordPress Content via Userrights or Time

WordPress comes with its own user administration, therefore we can control on the basis of various roles and objects the content. In some cases, it is a typical request that you give access to a complete content only to registered users. The same is possible only for parts of the content. There are certainly different possibilities to do this – I’m going to mention some solutions here.

User Rights

A simple solution is to query the rights of the user – the WordPress function current_user_can() makes it possible and it is simple to implement in your theme template. Therefore you can control that only logged in user are able to access the content.
Simple example how to implement it in a template:

if ( current_user_can( 'manage_options' ) )
	the_content( the_title( '', '', false ) . ' ' . __( 'read  more»', FB_BASIS_TEXTDOMAIN ) );
	_e( 'Only for logged in users', FB_BASIS_TEXTDOMAIN );

There are many solutions which are considering the different roles and objects. You can find a nice overview of objects per role at Codex.

Proof of Login via Function

Now the integration in the template is quite simple, but you normally have to adjust every template. Since WordPress uses hooks, you can also hook into and via Plugin or functions.php put a function in your template.

The following function is as an example and examines regardless of the user rights only if the user is logged in – if so, content may be seen. With the help of the hook the_content it controls it – so that the output in the theme, which comes via the template tag the_content() is only visible if you are logged in as a user.
Also explicitly in your feed, the output is only visible when you are logged in – !is_feed().

As an alternative for non logged users a section of the content is displayed – there are different approaches, here it was done explicitly with characters. It creates kind of a teaser for the reader.

function content_only4logged_in($content) {
	// all logged in User
	if ( is_user_logged_in() && 
			!is_null($content) && 
		 ) {
		return $content;
	} else {
		$content  = wp_html_excerpt( $content, 80 );
		$content .= ' …';
		$content .= __( '
Sorry, more of this content is only available for logged users.', FB_TEXTDOMAIN ); return $content; } } add_action( 'the_content', 'content_only4logged_in' );

Restrict Content via ShortCode

Another alternative is to make accessible only a certain section of the content only for logged in users. This could be done for example through the shortcode API of WordPress. In our example we use the shortcode [member] . Everything within this element is visible only to logged in users.
Also, this code belongs in a Plugin or in the interface of the themes for new features.

function fb_check_logged_in( $atts, $content = null ) {
	if ( is_user_logged_in() && 
		!is_null($content) && 
		 ) {
		return $content;
	} else {
		return __( 'Sorry, this content is only available for logged users.', FB_TEXTDOMAIN );
add_shortcode( 'member', 'fb_check_logged_in' );

Time-dependent output

A possible alternative is to output a content for a specific period. This possibility can be safely extended to any size. So it’s worth to use a Metabox to enter a date on the articles to query the time frame in a shortcode.

function content_only4time($content) {
	$date = strtotime( '01-10-2010' ); // day-month-year
	$now  = strtotime( date('d-m-Y') );
	echo $now . ' - ' . $date;
	if ( $date && ( $date < $now ) ) {
		return $content;
	} else {
		return __( '
Sorry, the time is not ripe.', FB_TEXTDOMAIN ); } } add_action( 'the_content', 'content_only4time' );

In this case you can use the Plugin wpSleep , which controls via shortcode and parameters the time frame of the output.


The list of approaches and possibilities are endless. With the possibilities you may have your own ideas and can use the above mentioned examples. You are welcome to comment for further ideas and possibilities.





4 responses to “Control WordPress Content via Userrights or Time”

  1. MR S J WEBLIN Avatar

    I am interested in the paragraph “Restrict Content via ShortCode” if I wanted more than 1 type of member could I just extend the code at the line:

    add_shortcode( 'member', 'fb_check_logged_in' );


    add_shortcode( 'goldmember', 'silvermember', 'fb_check_logged_in' );

    then mark up content with either ‘silvermember’ or ‘goldmember’ shortcode to restrict content visibility.

  2. Chris Avatar

    could I just extend the code at the line[…]

    That won’t work, because add_shortcode just excepts 2 parameters.

    Take a look at for more information. What you can do is adding both groups like this:

    add_shortcode( 'goldmember', 'fb_check_logged_in' ); add_shortcode( 'silvermember', 'fb_check_logged_in' );

    In this case both groups would have access to the posts content, but you can’t seperate these groups, because these groups don’t really exist in wordpress. Maybe there’s a plugin for handling different groups than the std wordpress groups, but with a normal wordpress installation you won’t be able to create these groups and seperate them from each other.

  3. Frank Avatar

    this is not possible; add_shortcode cant use a array or more as two parameters; also you can use in a loop:

    foreach( array('member', 'silvermemeber') as $shortcode )
    add_shortcode( $shortcode, 'fb_check_logged_in' );

  4. George Avatar

    I also had the add_shortcode issue, I enjoy this blog, nice work