0

I want to block the category page so that only logged in users can view the category page. I'm not talking about the posts in the page, I'm meaning the actual page view.

For instance, if I have a post category called "support", anyone can type in "/category/support/" after the domain name and view the posts in that category, but I need to restrict it so that only logged in users can see view that category page.

How can this be done? I don't need log in forms or anything special, just to block viewing of the category pages. I've tried adding custom meta keys into the category template that link with the wp-members plugin I've installed, and tried some other solutions including this one.

I can't find any plugins to do this, and no other code I've tried has worked yet. If anyone can help, that would be much appreciated!

0

There are plenty answers on WPSE about restricting page views based on logged in users or visitors.

what you should do is first check if user is logged in then check if you are on your restricted category and redirect on another page (maybe home or login page) if user is not logged in.

something like this in your functions.php file.

add_action( 'template_redirect', 'wpse_restrict_support');
function wpse_restrict_support(){

  if( ! is_user_logged_in() && is_category( 'support' ) ) {
    wp_redirect( '/wp-login.php' );
    exit;
  }
}

check out is_category(), is_user_logged_in() and wp_redirect() for more info

  • Thanks very much for your help, however that's not working at the moment. Do I need any additional plugins or to add any additional code in the template file? I'll carry on tinkering around with this but any help would be great! (most solutions I'm seeing work on the post meta, which category views don't have as far as I know?) – 5Diraptor Feb 24 '17 at 8:26
  • I didn't test at first, but now I did, and it's actually working for me. If support is not a custom taxonomy term (if it's in the default WP categories taxonomy) then it should be working for you too. Test again with my edited answer – bynicolas Feb 24 '17 at 18:24
  • I think that works :) I will test properly and mark as resolved later if so! Thanks! Is it possible to use this method to disable RSS feeds too? I'm a bit wary of using plugins. – 5Diraptor Feb 28 '17 at 14:35
  • Good. For feeds I haven't tested it but it should work the same if you use the is_feed() check. Just add another condition. Something like if( ! is_user_logged_in() && ( is_category( 'support' ) || is_feed() ) ) { ... } – bynicolas Feb 28 '17 at 17:57
  • Yes works great! Many thanks! I'll give the rss one a go, see if I can get it to work. – 5Diraptor Feb 28 '17 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.