I have had a developer create some code below in my wordpress footer.php file to predefault a set of fields based on the user’s profile.
The text fields seem to default fine however the select option (gender) does not default and the user has to select this option, even though the value is stored against their profile.
Is there a way to update this code to have the gender default? I think it is something to do with using .val()??
Here is the code:
<?php
$current_user = wp_get_current_user();
/**
* @example Safe usage: $current_user = wp_get_current_user();
* if ( !($current_user instanceof WP_User) )
* return;
*/
$username = $current_user->user_login;
$email = $current_user->user_email;
$firstname = $current_user->user_firstname;
$lastname = $current_user->user_lastname;
$displayname = $current_user->display_name;
//$user_id = $current_user->phone;
if ( is_user_logged_in() ){
global $current_user;
get_currentuserinfo();
$email = $current_user->user_email;
$name = $current_user->user_firstname.' '.$current_user->user_lastname;
$golflink = $current_user->dbem_golf_link;
$home_club = $current_user->dbem_home_club;
$handicap = $current_user->dbem_handicap;
$gender = $current_user->dbem_gender;
}
?>
jQuery( document ).ready(function() {
jQuery('#attendee_name').val("<?php echo $firstname.' '.$lastname; ?>");
jQuery('#golflink').val("<?php echo $golflink; ?>");
jQuery('#home_club').val("<?php echo $home_club; ?>");
jQuery('#handicap').val("<?php echo $handicap; ?>");
jQuery('#gender').val("<?php echo $gender; ?>");
Any advice would be great.
From other reading, can I just change:
jQuery(‘#gender’).val(“”);
to
jQuery(‘select#gender’).val(“”);
I tried the above but no changes.
The select I created in wordpress was ‘Male’ and ‘Female’
Thanks
To give value for a
select
in jquery, the value you are passing should be same as the value of the select box.Example 1:
Then
$gender
has value asmale
, so male will be selected.Example 2:
Then
$gender
has value as0 or 1
, But select box value ismale
andfemale
. So even though you have passed the value, it will not work.