0

I have added a field to my login form with the login_form action hook as below

add_action('login_form','select_contest_field');
function select_contest_field(){
//Output your HTML
?>
<p class="tml-user-pass-wrap">
  <label for="contest">Select Contest</label>
  <select class="form-control input-lg" id="contest" name="contest" required>
    <option value="uppa"  >UPPA 2017</option>
    <option value="ypa">Young Photographer Award</option>
  </select>
</p>
<?php
}

I have a login_redirect filter to redirect users on login depending on certain conditions and which option they choose.

function my_login_redirect( $redirect_to, $request, $user ) {
//set variables
global $wpdb;   
$contests_table = $wpdb->prefix.'photocontests';
$user_meta_table        = $wpdb->prefix.'usermeta';
$site_url   =   get_site_url();
$my_info_url = $site_url.'/my-information/';

if (isset($_POST['contest']) && $_POST['contest'] = "ypa"){
    $contest_page = $site_url.'/young-photographers-award-2017/young-photographers-award-2017-upload-submit/';
}
if (isset($_POST['contest']) && $_POST['contest'] = "uppa") {
    $contest_page = $site_url.'/uganda-press-photo-award-2017/uganda-press-photo-award-2017-upload-submit/';
} 

global $current_user; 
get_currentuserinfo(); 
$userID =   $user->ID;
$user_info_updated = $wpdb->get_var($wpdb->prepare("SELECT meta_value FROM $user_meta_table WHERE user_id = %d AND meta_key = 'userinfo_updated' ", $userID) );
//check if user has updated information
//is there a user to check?
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    //check for admins
    if ( in_array( 'administrator', $user->roles ) ) {
        // redirect them to the default place
        return $redirect_to;
    } else {
        if ($user_info_updated != 1){
        return $my_info_url;
        } else {
        return $contest_page;
        }
    }
} else {
    return $my_info_url;
}
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

When a user logins in and selects an option on the login form, they are always redirected to the $contest_page second option no matter what option is selected, in this case,

if (isset($_POST['contest']) && $_POST['contest'] = "uppa") {
$contest_page = $site_url.'/uganda-press-photo-award-2017/uganda-press-photo-award-2017-upload-submit/';

}

How can I make sure the right value for $contest_page is selected? Is the login form passing the selected option to the function properly? Basically, someone help me!

0

The problem is that you have a mistake in your IF logic, it should have two == and not one. As it is, the first IF is giving the value ypa to $_POST['contest'] instead of comparing.

The block of IFs should be:

if (isset($_POST['contest']) &&  "ypa" == $_POST['contest']){
    $contest_page = $site_url.'/young-photographers-award-2017/young-photographers-award-2017-upload-submit/';
}
if (isset($_POST['contest']) &&  "uppa" == $_POST['contest']) {
    $contest_page = $site_url.'/uganda-press-photo-award-2017/uganda-press-photo-award-2017-upload-submit/';
}

I'm also reversing the order of the parameters because this mistake happens a lot with programmers and by making the conditions in reverse order, if you write only one = by mistake there is a syntax error and it doesn't compile: Yoga conditions

  • OMG! Thank you @dbeja. Rookie mistake I made, hopefully it won't happen again. :-) – rhinorck Jul 28 '17 at 6:09

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.