skip to Main Content

Hello and thanks for spending the time to try and help me out. I am not experienced at all in PHP. I am attempting to do some custom snippets for my wordpress site. I have done a few already and this one for some reason isn’t working. I use Formidable Forms on my site to sign users up and at the time of sign up there is a custom meta field created called "info" and the value is set to "no". This field is only changed when the user creates an entry in my customer-info form. Then the meta value is changed to "yes". This snippet is to force them to fill out the info form when they log in and direct them to that page but for some reason the value I get from the user meta is "no". which is wrong. any ideas? Code im using:

function my_logged_in_redirect() {
$userid = get_current_user_id();
$info = get_user_meta($userid, 'info', true);
$loggedin = is_user_logged_in();
if (is_page(1795)){
    if ($loggedin == true && $info = 'no'){
        wp_redirect(home_url('/dashboard/account-information'));
    }
    else if ($loggedin == true && $info = 'yes'){
        wp_redirect(home_url('/dashboard'));
    }
    else if ($loggedin == false){
        wp_redirect(home_url());
    }
}

}
add_action( ‘template_redirect’, ‘my_logged_in_redirect’ );

2

Answers


  1. Well I think that right off the bat your if statement might be wrong. if ($loggedin == true && $info = 'no'){

    should be changed to if ($loggedin == true && $info == 'no'){

    also this else if ($loggedin == true && $info = 'yes'){

    to this else if ($loggedin == true && $info == 'yes'){

    Your code doesn’t look wrong aside those points ^. Looks like the $info might have the correct value but just the if statement was incorrect.

    Login or Signup to reply.
  2. Your code is incorrect.
    The operation of comparing two values is done in php with == and ===.

    • Equal to only value (==)
    • Equal amount and data type (===)
    • The operator (=) is used to set the value

    This code is correct:

    if (is_page(1795)) {
        if ($loggedin == true && $info == 'no') {
            wp_redirect(home_url('/dashboard/account-information'));
        } else if ($loggedin == true && $info == 'yes') {
            wp_redirect(home_url('/dashboard'));
        } else if ($loggedin == false) {
            wp_redirect(home_url());
        }
    }
    

    For more information, refer to this link

    https://www.w3schools.com/php/php_operators.asp

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search