2

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?

4

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

| 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
0
 <?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.

| improve this answer | |
  • if i used this condition then i got "Warning: Cannot modify header information" message. – Husain Ahmed Sep 17 '16 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy