How to redirect non-logged in users to a specific page?

Here are 2 examples which you will need to modify slightly to get it working for your specific needs.

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') && $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php' ) {

wp_redirect( 'http://www.example.dev/page/' ); 
    exit;
   }
}

Put this in your child theme functions file, change the page ID or slug and the redirect url.

You could also use code like this:

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

You can add the message directly to the page or if you want to display the message for all non logged in users, add it to the code.

http://codex.wordpress.org/Function_Reference/wp_redirect

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)