Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

User Profile Custom Meta - Display as drop down WordPress

  • SOLVED

Hello,

I am creating custom user meta fields which display on my profile page.

I have successfully configured them like so through my functions PHP.

function my_user_contactmethods($user_contactmethods) {

// You can get rid of ones you don't want

unset($user_contactmethods['jabber']);
unset($user_contactmethods['yim']);
unset($user_contactmethods['aim']);

// And add any news ones. The array key is the meta key name, the text
// is however you want it labelled.

$user_contactmethods['Title'] = __('Title');

$user_contactmethods['Publication / Company'] = __('Publication / Company');
$user_contactmethods['Job Title'] = __('Job Title');
$user_contactmethods['Publication Location'] = __('Publication Location');

$user_contactmethods['icl_admin_language'] = __('Language');

// etc for each field you want to appear
return $user_contactmethods;
}
add_filter( 'user_contactmethods', 'my_user_contactmethods');


But my problem is...

These user meta fields below are displaying as standard text inputs.


$user_contactmethods['Title'] = __('Title');

$user_contactmethods['icl_admin_language'] = __('Language');


I need the above to display as drop downs, instead of input boxs.

The drop down needs to have a label and value.

For example, for my meta_key: <strong>icl_admin_language</strong>

<select>
<option value="en">English</option>
<option value="es">Español</option>
<option value="it">Italiano</option>
</select>


How do I apply this to my function?

Thanks
Josh

------------------------------------------------

I had a guess, but no worky...


$user_contactmethods['icl_admin_language'] = __('Language'), true);
?><tr>
<td>
<select name="icl_admin_language" id="icl_admin_language" >
<option id="en"<?php selected( $user_contactmethods->icl_admin_language, 'en' ); ?>>English</option>
<option id="es"<?php selected( $user_contactmethods->icl_admin_language, 'es' ); ?>>Español</option>
<option id="it"<?php selected( $user_contactmethods->icl_admin_language, 'it' ); ?>>Italiano</option>
</select>
</td>
</tr><?php
}


Answers (1)

2012-01-29

Ivaylo Draganov answers:

Hello,

You are adding them to the contact methods array and it spawns only text fields. You should add them with the general action hooks. Using your example meta fields here's a sample code(duplicate the necessary chunks for other fields):
[[LINK href="http://pastebin.com/0zhWAtqY"]]http://pastebin.com/0zhWAtqY[[/LINK]]

In future versions of WP this is going to be easier, but until then we're stuck to such code :--)


Josh Cranwell comments:

Thanks Ivaylo

Lets I want to redo my...

<strong>$user_contactmethods['Publication Location'] = __('Publication Location');</strong>

...in the same way so all my info is grouped together. Is it OK to have IDs with spaces?

example...

<select name="Publication Location" <strong>id="Publication Location"</strong>>




Josh Cranwell comments:

Also I get a server error I use the code, does it work for you?


Ivaylo Draganov comments:

<blockquote>...in the same way so all my info is grouped together. Is it OK to have IDs with spaces?</blockquote>

I'm not sure if space are allowed in IDs or in meta field names. It's better to not use spaces. Check how exactly are your current meta fields stored - if they contain spaces you can change them.

I made some edits to the code - there were some errors. I've tested it and it saves properly.


Josh Cranwell comments:

Thank you this worked...

If I add some extra funds can you help me with this...

I'm using the WP Front End plugin which pulls in the profile page onto the front end of my site.

I add this code...

<tr>

<th><label for="icl_admin_language"><?php _e( 'Language' ); ?></label></th>
<td>
<?php $value = get_the_author_meta( 'icl_admin_language', $user->ID ); ?>
<select name="icl_admin_language" id="icl_admin_language">
<option value="en" <?php selected( $value, 'en' ); ?>>English</option>
<option value="es" <?php selected( $value, 'es' ); ?>>Espanol</option>
<option value="it" <?php selected( $value, 'it' ); ?>>Italiano</option>
</select>
</td>

</tr>


on <strong>line 160</strong> in <strong>wpuf-editprofile.php</strong>

I've attached the plugin (my version) here [[LINK href="http://wtrns.fr/IU8ZCHJb1SFujda"]]http://wtrns.fr/IU8ZCHJb1SFujda[[/LINK]]


The select field now appears on my front end...

but when I submit, it does not register the changes. I'm not sure where to put the code so when the form submits, it registers this field.

If you can figure it out, can you tell me where to add the code so I can do it for my other custom meta fields.

Thanks


Josh Cranwell comments:

To add the profile edit form to a post...

Add this short code <strong>[wpuf_editprofile]</strong>


Josh Cranwell comments:

Figured it out, thanks for you help.

Help me lots!