I have the following snippet of code that I am experimenting to restrict access to certain wordpress pages through a plugin.
It works... almost. The browser is redirected to the login page, but the query string is wrong. The query string has the id and slug for the login page and not the page it was redirected from. I want to use this info to redirect back to the page that sent the user to the login page.
add_action( 'template_redirect', 'redirect_to_login_page' );
function redirect_to_login_page() {
if ( is_user_logged_in() || is_page('login') ) return;
$postId = get_queried_object_id();
$slug = get_queried_object()->post_name;
wp_redirect( '/login?r='.$postId.'&s='.$slug, 302 );
exit;
}
Can anyone tell me why this does not give me the id and slug from the original page?