how do i add a multi select array to wordpress functions

I have been trying to add a multi select array from another plugin into the functions.php of a wordpress theme or a separate plugin. the field is “US_States_Serviced” i can get the code to know the the wp_usermeta is there, but returns ARRAY in the text field. I am assuming i need to ad the ARRAY. which i have no clue how to do. I looked, and searched but no codes really look uniform to me and match what i want as far as an array. Cant the plugin, or functions.php pull the array automatically if i set the TYPE= correctly? Is multi select not native to WordPress. Just lost

the “US_States_Serviced” field located in the wp_usermeta has all US STATES that allows a user to select however many he services. Now i need to reflect this data into the user profile of wordpress. I been trying 2 approaches. Which is best, and can some one help me please.

Read More

OPTION 1 ADD TO FUNCTIONS.PHP

    add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>

<table class="form-table">
<tr>
<th><label for="US_States_Serviced"><?php _e("US State You Service"); ?></label></th>
<td>
<input type="text" name="US_States_Serviced" id="US_States_Serviced" value="<?php echo esc_attr( get_the_author_meta( 'US_States_Serviced', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter all states serviced."); ?></span>
</td>
</tr>
<tr>
<th><label for="city"><?php _e("City"); ?></label></th>
<td>
<input type="text" name="city" id="city" value="<?php echo esc_attr( get_the_author_meta( 'city', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your city."); ?></span>
</td>
</tr>
<tr>
<th><label for="province"><?php _e("Province"); ?></label></th>
<td>
<input type="text" name="province" id="province" value="<?php echo esc_attr( get_the_author_meta( 'province', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your province."); ?></span>
</td>
</tr>
<tr>
<th><label for="postalcode"><?php _e("Postal Code"); ?></label></th>
<td>
<input type="text" name="postalcode" id="postalcode" value="<?php echo esc_attr( get_the_author_meta( 'postalcode', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your postal code."); ?></span>
</td>
</tr>
</table>

OPTION 2 AS A PLUGIN

class shw_user_meta {

 function shw_user_meta() {
 if ( is_admin() )
 {
 add_action('show_user_profile', array(&$this,'action_show_user_profile'));
 add_action('edit_user_profile', array(&$this,'action_show_user_profile'));
 add_action('personal_options_update', array(&$this,'action_process_option_update'));
 add_action('edit_user_profile_update', array(&$this,'action_process_option_update'));
 }

 }

 function action_show_user_profile($user)
 {
 ?>
 <h3><?php _e('EXTRA PROFILE INFORMATION') ?></h3>

 <table>
 <tr>
 <th><label for="member_id"><?php _e('Member ID'); ?></label></th>
 <td><input type="text" name="member_id" id="Memmber ID" value="<?php echo esc_attr(get_the_author_meta('member_id', $user->ID) ); ?>" /></td>
 <th><label for="us_states"><?php _e('US States you Service'); ?></label></th>
 <td><input type="multi" name="US_States_Serviced" id="US States you Service" value="<?php echo esc_attr(get_the_author_meta('US_States_Serviced', $user->ID) ); ?>" /></td>
 <th><label for="user-registered"><?php _e('User Registered'); ?></label></th>
 <td><input type="text" name="user_registered" id="user_registered" value="<?php echo esc_attr(get_the_author_meta('user_registered', $user->ID) ); ?>" /></td>
 </tr>
 </table>
 <?php
 }

 function action_process_option_update($user_id)
 {
if(isset($_POST['member_id'])){
update_user_meta( $user_id, 'member_id', $_POST['member_id'] );
}

if(isset($_POST['us_states'])){
update_user_meta( $user_id, 'us_states', $_POST['us_states'] );
}

if(isset($_POST['user_registered'])){
update_user_meta( $user_id, 'user_registered', $_POST['user_registered'] );
}
 }
}
/* Initialise outselves */
add_action('plugins_loaded', create_function('','global $shw_user_meta_instance; $shw_user_meta_instance = new shw_user_meta();'));            
?>

Related posts

Leave a Reply