WordPress hacks: Members only content shortcode

Have you seen the content lockers that require you to either tweet or like a post etc in order to view a link or a piece of information? This little hack will integrate a shortcode that hides information from people that aren’t logged in at your site.

Add this to your functions.php¬†file …

function member( $atts, $content = null ) {
 if ( is_user_logged_in() && !is_null( $content ) && !is_feed() ) {
 return $content;
 return '';
 } else {
 
 $yonlendir = get_permalink();
 $form = wp_login_form(array('echo' => false, 'redirect' => $yonlendir ));
 return $form;
 }
}
add_shortcode( 'mem', 'member' );

Then when you write a post or add a page, simply wrap the [ mem ] and [ /mem ] shortcode tags around the content you want to hide and when a user views the post/page, they will have a login form where the content should be if they aren’t already logged in.

Leave a Reply

Your email address will not be published. Required fields are marked *