• Resolved bazingauser

    (@bazingauser)


    Hi guys, I was wondering if someone could help me to hide a specific page from non-logged in users. I use paid membership pro plugin and it will kind of protect the content. But the information to be held on this page is very sensitive so I would rather for the page not at all to be visible on the menu for guests. Let’s say I have three pages provided on the homepage menu: Welcome, Contact us and Private info – so I would like the Private info only to be visible if logged in.
    If anyone could spare a little time to help me with the code, I would be sincerely grateful.
    Thank you very much in advance.
    Kind regards

Viewing 15 replies - 1 through 15 (of 16 total)
  • In your functions.php file add this code after changing XX to your page ID

    add_filter( 'wp_head', function(){
    	if (is_page(XX) && !is_user_logged_in()) {
    		add_filter('the_content', function(){
    			return __('Sorry! Onlu logged-in user are allowed to see the content');
    		}, 99);
    	}
    });
    Thread Starter bazingauser

    (@bazingauser)

    Hi Sidati, thank you for your time. The above code blanks the entire homepage. I have changed the page name in brackets and checked the symbols… All matched, just didn’t work ??

    if you still using an old php version < 5.4, then this code will not work ??
    but this will, CHANGE THE XXXXXXX WITH YOUR PAGE ID (interger).

    add_filter( 'wp_head', 'wp_995454');
    
    function wp_995454(){
    	if (is_page(XXXXXXXXXX) && !is_user_logged_in()) {
    		add_filter('the_content', 'wp_35465', 99);
    	}
    }
    
    function wp_35465(){
    	return __('Sorry! Only logged-in users are allowed to see the content');
    }
    Thread Starter bazingauser

    (@bazingauser)

    … you will hate me, but it still does not work ?? And I have typed the page name exactly as it appears on a home page. I use php 5.4.30.
    Thank you

    Not the page name but the page ID which its a number, you can get it from address bar (e.g. the ID page is 104)

    Where to get the page ID

    the code will work ONLY if NON-LOGGED user visit that specific page so make sure you are no logged-in :p

    Thread Starter bazingauser

    (@bazingauser)

    So embarrassing but still doesn’t work :S My page ID is 1021. I refreshed the page when non-logged in and it just went blank… Never mind. Thank you so much for trying. I am sure it’s me who is doing something wrong. Thank you Sidati, for your help ??

    I tested it twice and it works for me, so i couldn’t be more helpful ??
    maybe someone else have have another idea ??

    Thread Starter bazingauser

    (@bazingauser)

    You have a very good sense of humour, great examples ?? em – you are very helpful, yes. It’s probably me who is not doing something right. It’s 1:30 in UK so probably just tired, most likely to get it right tomorrow ?? oh, hang on… Could this be because I have it protected with paid membership??

    Thread Starter bazingauser

    (@bazingauser)

    Hi Sidati, got it working ??
    Thanks so much for your time and for all of the effort trying to help me ??
    I do understand that beginners sometimes are impossible ??
    Kind regards

    @bazingauser glad you sort it out, can you please share the fix with the community ?? ??
    & don’t forgot to mark this topic as RESOLVED

    Worked perfectly for me, Sidati. I just changed the ID as requested, and bam, it worked right out the gate. Thanks again.

    Peace.

    would you need to add the code for every page you want to only have viewed by registered members, or is there a way to do the above coding for multiple pages without adding repetitive coding?

    Hi @nikkiapt,
    For multiple pages use, i suggest to create a template page like so :

    <?php 
    /**
     * Template Name: Protected page
     */
    
    get_header();
    ?>
    
    <?php if is_user_logged_in() : ?>
       <article>
          <h1><?php the_title() ?></h1>
          <div><?php the_content() ?></div>
       </article>
    <?php else : ?>
       <p><?php _e('Sorry! Only logged-in users are allowed to see the content');
     ?></p>
    <?php endif ?>
    
    <?php get_footer(); ?>

    Then every page you want to make it protected, just assign this template to it, check out this link

    Thank you, @sidati! So, where would I put that code? ??

    Please checkout this link to understand the concept of “Page template” ;),

    TMALSS; you need to create new php file in your current theme and put that code in it and save it, then from the admin dashboard go to edit the page you want to protect and choose the template you just created.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Hide page from non logged in users’ is closed to new replies.