Hello everyone,,

I am trying to write a role-specific redirect snippet within my Wordpress Child Theme Functions .php file.

Essentially, I want to redirect users to an account login/creation page to prevent them from accessing or seeing the wholesale shop/cart/checkout (page IDs 470, 472, and 478) IF not logged in AND with proper role permissions.

The PHP snippet isn't functioning, and I am getting back " syntax error, unexpected '&&' (T_BOOLEAN_AND)"

Any help spotting or correcting where I am going wrong?

Thank you!

/ Redirect User FROM Cart/Checkout/Shop TO Account IF NOT logged in as a defined user role /

function cm_redirect_users_by_role() {

if ( ! defined( 'DOING_AJAX' ) ) {
 
    $current_user   = wp_get_current_user();
    $role_name      = $current_user->roles[0];
 
    if (('wholesale_customer' || 'admin' || 'shop_manager' ) != $role_name) &&
if (is_page('470' || '472' || '478')) {
        wp_redirect( '[redirect link here]' );
    } // if $role_name
 
} // if DOING_AJAX

} // cm_redirect_users_by_role
add_action( 'admin_init', 'cm_redirect_users_by_role' );

    Yeah; you've totally ignored how if statements and boolean operators work.
    Changing

     if (('wholesale_customer' || 'admin' || 'shop_manager' ) != $role_name) &&
    if (is_page('470' || '472' || '478')) {
    

    to

     if (('wholesale_customer' || 'admin' || 'shop_manager' ) != $role_name && is_page('470' || '472' || '478')) {
    

    Will fix your immediate problem, but then you'll have to deal with the fact that 'wholesale_customer' || 'admin' || 'shop_manager' and '470' || '472' || '478' are always true, leaving you asking

     if (true != $role_name && is_page(true)) {
    

      I think the most direct translation into functioning PHP would be something like:

      if(
          (
              $role_name != 'wholesale_customer' && 
              $role_name != 'admin' && 
              $role_name != 'shop_manager'
          ) && (
              is_page('470') || 
              is_page('472') || 
              is_page('478')
          )
      ) {
          // do redirect
      }
      

      You could maybe replace those $role_name checks with an in_array() check to clean it up a tiny bit, though the whole thing has a bit of a "code smell" to me that there might be a better way to implement this business logic?

        11 days later

        You generally shouldn't be combining strings with boolean operators. It may be helpful for you to see the output of this code:

        echo ("wholesale_customer" || "admin" || "shop_manager" );

        The result of this echo statement just outputs 1. I.e., the number one. The other guys are correct that you want to check if $role_name is equal to one of those strings. You might want to use in_array for this:

        $acceptable_role_names = array("wholesale_customer", "admin", "shop_manager");
        if (in_array($role_name, $acceptable_role_names)) {
          echo "well at least the role name is correct.";
        }

        But then you also need to check the is_page bit, too. As you probably have surmised, your IF statement is getting pretty complicated. You might be better off writing a function or two so you can simplify the syntax of your IF statement to make it more readable.

          Write a Reply...