I'd like to redirect user to login page on specific pages, after displaying message "You need to login to view this page". I followed this documentation, but couldn't get through. https://codex.wordpress.org/Function_Reference/auth_redirect

2 Answers 2


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/' ); 

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 );

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.

  • Thank you the_dramatist. Can you tell me how to redirect user back to same page, from which user has been redirected to login page, after user has logged in for frontend?
    – dhiraj
    Commented Dec 5, 2016 at 12:04

It works fine in our file, will you please give us bit of code in which filter or action you are running this code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.