While doing a website for a client of mine, one of the requirement was to show content to subscribers or members only. Although there is a plugin for this but I stumble upon a much simpler script. For more details, you can visit justinandlock.com for the full how to.

Since the tutorial only teach you how to create them through functions.php inside your theme files, I’ve made some modification towards the aesthetics of it. Obviously, we would want to display the login and register link for easy access to our viewers. Here is my code which you can place them inside functions.php located in your theme folder. If it’s not there, just create one.

/* Members only */
add_shortcode( 'member', 'member_check_shortcode' );

function member_check_shortcode( $attr, $content = null ) {

if (is_user_logged_in() && !is_null($content) && !is_feed()) {
//REGISTERED USER
return do_shortcode($content);

} else {
//UNREGISTERED USER
$redirect = get_permalink();
$login = get_bloginfo('url').'/wp-login.php?redirect_to='.$redirect;
$register = get_bloginfo('url').'/wp-login.php?action=register&redirect_to='.$redirect;
return '
<blockquote class="clearfloat">The following content is available for subscribers only. Registration is free!
<span class="membersonly floatr"><a rel="nofollow" href="'.$login.'">Login</a> | <a rel="nofollow" href="'.$register.'">Register</a></span></blockquote>
';
}

}

The usage of the code is simply by wrapping the content with [members] and close it with [/members] just as you would with any other short code. If you need to change the looks of the “warning” message, change the second return to your HTML code by using the variable $redirect, $login and $register for the links.

Leave a Reply