0

Following code is working fine for pages but how to add category id as well with pages in the following code.

add_action( 'template_redirect', function() {

  if ( is_user_logged_in() || ! is_page() ) return;

  $restricted = array( 250, 253 ); // all your restricted pages

  if ( in_array( get_queried_object_id(), $restricted ) ) {
    wp_redirect( site_url( '/user-registration' ) ); 
    exit();
  }

});
1

In you actual code you can see this:

if ( is_user_logged_in() || ! is_page() ) return;

This code does this: if user is logged in or is not in a page, then return/do nothing. You need to remove ! is_page():

add_action( 'template_redirect', function() {

    if ( is_user_logged_in() ) return;

    $restricted = array( 250, 253 ); // all your restricted pages

    if ( in_array( get_queried_object_id(), $restricted ) ) {
        wp_redirect( site_url( '/user-registration' ) ); 
        exit();
    }

});

Alternatively you can leave is_page() and add also the check for is_category() as your needs.

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.