in WordPress

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]

Upgrade to access all content on Paulund

Members unlock all tutorials and snippets

Access to all downloadable content

Access to code examples before others

Sign Up Now

Already a member? Login here

Subscribe To The Weekly Newsletter

Get weekly updates to your email