Home Wordpress Hooks How to hide wordpress content from non logged in users?

How to hide wordpress content from non logged in users?

by therichpost
7 comments
How to add custom meta title and meta description in Wordpress?

Hello, welcome to therichpost.com. In this post, I will tell you, How to hide wordpress content from non logged in users? WordPress is the best cms. WordPress hooks(add_action, add_filter) give us the power to edit or change the code without interruption into the files and this is the best thing about wordpress. Now I am going to tell you how the hooks work.

I have made shortcode to hide content from non logged in users.
Here is the working wordpress hook¬† to hide wordpress content from non logged in users and you need to add this into your theme’s functions.php file:
// functions.php file code
add_shortcode( 'member', 'member_check_shortcode' );
function member_check_shortcode( $atts, $content = null ) {
     if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
     return $content;
     return '';
}

//Here you can use this hook into wordpress dashboard post or pages :
[member]
Plugin Code
[/member]

//if you want to use it in a PHP template file, you can do it like this:
echo do_shortcode( '[member]' . $youcodetext. '[/member]' );

 Now you are done and if you have query related to this post or you want to do some more with this code then please do comment below and I will come with wordpress hooks.

You may also like

7 comments

Dennis September 12, 2020 - 12:42 pm

Hi there, nice! how would you alter that to hide content from logged-in users?

Reply
Ajay Malhotra September 12, 2020 - 3:13 pm

Hi, that is simple, please check this:
if ( !is_user_logged_in())
{
}

Reply
vish February 22, 2022 - 11:35 pm

Thanks for the snippet man. Really appreciated – Quick question, is there a way to redirect non-logged in users to the registration page (custom page) when they try to access a members only page? Thanks again.

Reply
Ajay Malhotra February 23, 2022 - 5:37 am

Try this
Hi, that is simple, please check this:
if ( !is_user_logged_in())
{
//register page link
}

Reply
giorgio October 20, 2022 - 1:57 pm

How do I put the shortcode on via php? thank you

Reply
therichpost October 22, 2022 - 8:15 am

Reply
giorgio October 20, 2022 - 1:58 pm

Come faccio a mettere lo shortcode su the_content(); via php?

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.