Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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.

2 Answers 2

up vote 0 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);
  exit;
}

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
 <?php
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

 
discard

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