Advertisement
meetsos

Restrict Custom Post Type or Taxonomy to Logged In Users

Apr 19th, 2016
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.95 KB | None | 0 0
  1. <?php
  2. // While specified areas are under development, redirect users to home page if not logged in
  3. add_filter( 'wp', 'f040925b_redirect', 0 );
  4. function f040925b_redirect( $content ) {
  5.     global $post;
  6.     if(
  7.         (
  8.             $post->post_type == '[custom_post_type_name]'
  9.             ||
  10.             is_post_type_archive( '[custom_post_type_name]' )
  11.             ||
  12.             is_tax( '[custom_taxonomy_name]' )
  13.         )
  14.         &&
  15.         !is_user_logged_in()
  16.     ) {
  17.         wp_redirect( get_home_url() );
  18.         exit;
  19.     }
  20.     return $content;
  21. }
  22.  
  23. /* Essentially, this runs three early checks:
  24.             1) Does the $post object's post type match what you're trying to hide?
  25.             2) Is this URL a post type archive for the post type you're trying to hide?
  26.             3) Is this URL a taxonomy/term page for the taxonomy you're trying to hide?
  27.  If any of these is a match and the user is not logged in, then the user is redirected to homepage. */
  28. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement