WordPress redirect to landing page if not logged in

I’m using the following code in my functions.php file to redirect users who are not logged in to a particular landing page

    <?php
if(!is_user_logged_in()) {
    wp_redirect( 'http://www.mysite.com/landingpage', 301 ); exit;
}

The problem is I cannot access my wp-login or wp-admin anymore. Every url redirects to landingpage. Is there a way I can exclude certain urls from redirecting?

2 Answers
2

something like:

if (
  !in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))
  && !is_admin()
  && !is_user_logged_in()
) {
  wp_redirect('http://www.mysite.com/landingpage', 301);
  exit;
}

should do it.

see http://codex.wordpress.org/Function_Reference/is_admin &
Check if wp-login is current page

Leave a Comment