skip to Main Content

I would like to display content to a certains kind of user (some of them custom) if they are logged-in.
Is there a way to associate :

if( is_user_logged_in() && ($user_role != "Administrator" && $user_role != "customrole") ):

// display content
       
 else:

// redirect back to other page
wp_redirect( 'https://customurl.com' ); 
    exit;

endif;

But when I try thsio code I have a fatal error. What do I do wrong please ?
Best regards,
Clément

2

Answers


  1. Chosen as BEST ANSWER

    Finally it was a mix, I used permission and a custom field if I want some part accessible to everyone :

    $access_to_everyone = get_field('access_to_everyone');
    // var_dump($access_to_everyone);
    if ( $access_to_everyone == true ) {
        $access = "granted";
    } else {
        if( is_user_logged_in() ) {
            if ( !current_user_can('administrator') ) {
                $access = "refused";
                }  
            } else {
                $access = "granted";
            }
        } else {
            $access = "refused";
        }
    }
    if ( $access == "granted" ) {
        display A;
    } else {
        redirection";
    }
    

  2. if you wanna use multiple conditions you have to remove the () afther your && in the if statement and try it like this

    if(is_user_logged_in() && $user_role != "Administrator" && $user_role != "customrole"){
    //display content
    }
    else{
    wp_redirect( 'https://customurl.com' ); 
    exit;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search