wordpress-display-a-message-or-other-text-in-a-page-or-post-only-to-logged-in-users.jpg

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 a shortcode to detect 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.

<?php
function user_only_shortcode($atts, $content = null)
{
    if (is_user_logged_in() &amp;&amp; !is_null($content) &amp;&amp; !is_feed()) {
        return $content;
    }
}
add_shortcode('user_only', 'user_only_shortcode');
?>

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

 

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

 

Feeling social? Share this with friends and colleagues...

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on skype
Skype
Share on whatsapp
WhatsApp
Share on email
Email