0
votes

I have the following code for my functions.php but it won't work how I want it to. I basically want it to hide the category unless the user is logged in, so basically redirect them to a login page

Code seems fine to me but wondering if there's any suggestions? I can't find ANY decent plugins to help me either

  add_action( 'init', 'check_redirect_page' );

function check_redirect_page() {
    if ( !is_user_logged_in() && !is_category( 177 ) ) {
        wp_redirect( home_url( '/login' ) );
        exit(); 
    }
}
1

1 Answers

0
votes

This part of your code ..

!is_user_logged_in() && !is_category( 177 )

.. means "If the user is not logged in and the category id is NOT 177" .. and it seems you are asking how to check if the non-logged user IS on that category.
If that is the case, just remove the ! in front of is_category.

Also, template_redirect might be a better hook to use.