Wordpress Snippets

Show Post Content Only to Logged-In Users

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]

Hire Us. Or just say Hi!
Need a job? Apply to get one.