I have a scenario where users have bookmarked a direct link to an event page (that has recurring events) and I want them redirected to a login page first. I’ve seen many examples of code online where you can redirect if a certain page is displayed, such as:
add_action( 'admin_init', 'redirect_non_logged_users_to_specific_page' );
function redirect_non_logged_users_to_specific_page() {
if ( !is_user_logged_in() && is_page('add page slug or ID here') {
wp_redirect( 'http://www.example.dev/page/' ); exit; }
}
I think I can get the redirect to work by making slight modifications to this example but I’m not sure when you view an event it is a page, event, or something else. Any tips you provide me?
Thanks,
Robert
Robert Stankey
I figured out how to make this work for what I needed…..
add_action( ‘template_redirect’, ‘redirect_non_logged_users_to_specific_page’ );
function redirect_non_logged_users_to_specific_page() {
global $wp_query;
if(get_query_var('post_type') == 'event') {
if(!is_user_logged_in() && ($wp_query->post->post_name == 'family-class' || $wp_query->post->post_name == 'black-belt-class-2')) {
wp_redirect( 'https://www.kimsacademy.com/classes-reservations-login/');
exit;
}
}
}
Thanks,
Robert
Robert Stankey