You can do it like this in admin section-
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 i.d here') &&
$_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php' ) {
wp_redirect( 'http://www.example.dev/page/' );
exit;
}
And for frontend-
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page('slug') && ! is_user_logged_in() ) {
wp_redirect( 'http://www.example.dev/your-page/', 301 );
exit;
}
}
And this auth_redirect
function is for backend. If you want to use it on front end then add the below filter-
add_filter( 'auth_redirect_scheme', 'the_dramatistcheck_loggedin' );
function the_dramatist_check_loggedin(){
return 'logged_in';
}
Then auth_redirect()
will work as expected : redirect users to the login form if they are not logged in.