WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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?

share|improve this question

migrated from stackoverflow.com Jun 27 '12 at 13:12

This question came from our site for professional and enthusiast programmers.

up vote 3 down vote accepted

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

should do it.

see http://codex.wordpress.org/Function_Reference/is_admin & http://wordpress.stackexchange.com/questions/12863/check-if-were-on-the-wp-login-page

share|improve this answer
Thanks for the response. This looks like it will do the trick but I'm getting a syntax error on line 5. Any ideas? – Ryso Jun 27 '12 at 0:57
yeah, sorry. missed the brackets. – Max Gherkins Jun 27 '12 at 0:57
which brackets? – Ryso Jun 27 '12 at 1:07
i already edited the answer. the ones behind is_user_logged_in where initially missing... was that it? – Max Gherkins Jun 27 '12 at 1:09
No, still getting the error on line 5 – Ryso Jun 27 '12 at 1:11
if($_SERVER['REQUEST_URI'] != '.../' || $_SERVER['REQUEST_URI'] != '.../'){

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

This basically tests to see if you are on a specific page, before executing the redirect, if the first statement returns true, and the user is not logged in it will redirect.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.