redirect specific page category if not logged in

I’m sure this was working but now it’s not. I added categories to pages with:

 function add_categories_to_pages() {
 register_taxonomy_for_object_type( 'category', 'page' );
 add_action( 'init', 'add_categories_to_pages' );

then tagged a range of pages with a ‘client’ category and added to functions:

add_action( 'template_redirect', 'client_redirect_to_login' );
function client_redirect_to_login() {
$category_slug = 'client';
global $pages;
if ( ! is_user_logged_in() && in_category( $category_slug, $pages ) ) {
wp_redirect( site_url( '/login' ) ); 

the intention, simply enough, is that not logged in users trying to directly access any of the ‘client’ pages is redirected to my custom /login page. being returned directly to the page they were trying to access would be a bonus but this, THIS, used to work. there’s a dozen pages and growing, so restricting by category is easier than by page ID array – but i can’t see what i’m doing wrong.

all advice greatly appreciated!

Read more here: redirect specific page category if not logged in

Leave a Reply

Your email address will not be published. Required fields are marked *