How to hide WordPress content from non logged in users?

When you build or create a Website with WordPress sometimes you need to hide content from the non-logged-in users. As sometimes you have some special thing for only who becomes your subscriber. Now we will check How to hide WordPress content from non-logged-in users?

How to hide WordPress content from non logged in users?

Method One:

Table of Contents

If you want only hide some content in the content editor, like some Download link, or Download Button. Which is only available for Logged in users. You can do it by shortcode. Just copy this below Shortcode, and add this in your theme’s functions.php file:

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

And use it in a content editor like this :

[only_loggen_in]
Only Logged in User can see this.
[/only_loggen_in]

Method Two:

If you are a developer, and if you want to load some content or template based on the user logged in status, you can simply do this as below example;

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

Like if you want to load different website titles between logged-in users and non-logged-in users. you can do like this example below:

if ( is_user_logged_in() ) {
    echo '<title>Hello user, Welcome to the daily WP.</title>';
} else {
    echo '<title>The daily WP</title>';
}

You can use it in any WordPress hooks, or in any WordPress template parts
Hope you can solve the problem with this, or if you need more complicated code, and you need to archive more complicated things, just comment below, I will try to solve it.

Add a Comment