1

I am trying to redirect users based on the following...

  • If Role = Customer & Page Is NOT ID 6 Then Redirect To Checkout
  • If Not Logged in & Page Is Not ID 6 then redirect to login
  • User role = member and Page ID is 6 redirect to home page

    //If Role = Customer & Page Is ID 6 Then Redirect To Checkout
    if ( user_can( $current_user, "customer" ) && ! is_page( 6 ) ) 
    {
    wp_redirect(home_url().'/checkout');
    exit;
    }
    
    elseif ( ! is_page ( 6 ) )
    {
    //If Not Logged in & Page Is Not ID 6 then redirect to login
    auth_redirect();
    }
    
    
    elseif ( user_can( $current_user, "member" ) && is_page( 6 ) )
    {
    // User role = member and Page ID is 6 redirect to home page
    wp_redirect(home_url());
    exit;
    }
    ?>
    

Which as far as I can see should work, but its not, it gives me a blank page. The script is being run from header.php and is the first thing in the PHP file.

The first rule - If Role = Customer & Page Is ID 6 Then Redirect To Checkout works correctly and they are redirected to the checkout page.

The second rule prevents anybody from logging in

The third rule works correctly

Am I doing something wrong?

1

EDIT

You are using auth_redirect();, by default it checks if a user is logged in or not, so you don't need to check again if a user is logged in or not

When this code is called from a page, it checks to see if the user viewing the page is logged in. If the user is not logged in, they are redirected to the login page. The user is redirected in such a way that, upon logging in, they will be sent directly to the page they were originally trying to access.

So you can just do

if ( ! is_page ( 6 ) ) {
    auth_redirect();
}

EDIT 2

//If Role = Customer & Page Is ID 6 Then Redirect To Checkout
if ( user_can( $current_user, "customer" ) && ! is_page( 6 ) ) 

This is your code's first two lines, check line one Page Is ID 6 and check line 2 ! is_page( 6 ). Spot the difference?

Your code in line 2 says if the page ID IS NOT 6

EDIT 3

The script is being run from header.php and is the first thing in the PHP file

What do you mean the first thing in the php file. This will break you site. You will need to move this code to at least within the opening body tag (<body>), where it should be

EDIt 4

Since we started, a few thing changed :-). Try to re-organise your sequence

//If Role = Customer & Page Is ID 6 Then Redirect To Checkout
if ( user_can( $current_user, "customer" ) && ! is_page( 6 ) ) {

    wp_redirect(home_url().'/checkout');
  exit;

}elseif ( user_can( $current_user, "member" ) && is_page( 6 ) {

   // User role = member and Page ID is 6 redirect to home page
     wp_redirect(home_url());
  exit;

} elseif ( ! is_page ( 6 ) && !is_user_logged_in() ) {
//If Not Logged in & Page Is Not ID 6 then redirect to login
     auth_redirect(); //Not sure if this will work. 
}
?>
  • Hi,I have tried amending things but it still isn't working for me. Curiously, any page on my site that has the number 6 anywhere in the title is also blank. Connected? – fightstarr20 Aug 4 '14 at 19:36
  • 1
    Getting tired, mind a bit lazy, lol. Check my update – Pieter Goosen Aug 4 '14 at 19:45
  • Good point, I have changed it again but it is still not working as expected. I have updated my original post with my current code – fightstarr20 Aug 4 '14 at 19:52
  • 1
    are you published the page which have ID 6? if page is in draft or trash then it'll not work. – Chinmoy Kumar Paul Aug 4 '14 at 19:54
  • 1
    See my update @fightstarr20 – Pieter Goosen Aug 4 '14 at 20:05
0

Don't do this in your header.php and pollute your templates, make use of the system WordPress offers you and hook into the template_redirect action like this:

add_action( 'template_redirect', 'wpse156774_redirect' );
function wpse156774_redirect() {
    // your code and conditions here
}

This can be put into your functions.php or a plugin.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.