Show Post Content Only to Logged-In Users

How to Show Post Content Only to Logged-In Users

in Shortcode
Google+ LinkedIn

Sometime you want to reward your registered user of your website with a special gift available only to them or maybe you have a important announcement/update for them or you simply want to send a message only to them. To accomplish that, this code snippet below creates a new shortcode with which to wrap content in order to hide it from casual visitors but not logged-in users.

Just copy and paste this snippet in your theme’s functions.php file.

// show post content only to logged-in users
function wcs_content_loggedin( $atts, $content = null ) {
    if( is_user_logged_in() ) return '<p>' . $content . '</p>';
    else return;
}
add_shortcode( 'loggedin', 'wcs_content_loggedin' );

After that you can use shortcode in your content to display special message/content only to logged-in users. Here is a example.

// shortcode usage
[loggedin]
    Your content only for logged-in users.
[/loggedin]

Share the love

If you like this snippet, share it with others!

Leave a Reply

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