Display Text To Only Logged In WordPress Users

Wordpress

You can use this to post advice or reminders to logged in users when they need to perform different tasks.

All you have to do is add the following anywhere in your theme you want to display the message.

<?php

if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
    echo 'Welcome, visitor!';
};
?>

Create Shortcode For Logged In Users

To turn this into a shortcode so you can use it in your posts you can just use the below snippet add this into your functions.php page.

function member_only_shortcode($atts, $content = null)
{
    if (is_user_logged_in() && !is_null($content) && !is_feed()) {
        return $content;
    }
}
add_shortcode('member_only', 'member_only_shortcode');

To use this shortcode in your post use the following code.

[member_only]User is logged in.[/member_only]

Advertise here 50% Off

Comment