When you are creating a WordPress plugin to play with users’ profile meta data, you may need to use some different meta data those are not exists in user profile page. So you must want to have new custom meta field with user profile. Sometimes we call it user custom field. How to do add this custom field? Here I will show you the complete code snippet and a simple demo plugin.
Suppose you want to add an extra custom field to store user’s twitter account username. By default this field is not exists in profile page ( As WordPress version 3.5.1). Add following code snippet in your plugin file or in theme functions.php. It will show an extra input field in user profile edit page. It will add a new meta key and value in the WordPress user meta table wp_usermeta in database. This new key will be loaded with user object.
add_action( 'show_user_profile', 'oneTarek_extra_user_profile_fields' ); add_action( 'edit_user_profile', 'oneTarek_extra_user_profile_fields' ); add_action( 'personal_options_update', 'oneTarek_save_extra_user_profile_fields' ); add_action( 'edit_user_profile_update', 'oneTarek_save_extra_user_profile_fields' ); function oneTarek_save_extra_user_profile_fields( $user_id ) { if ( !current_user_can( 'edit_user', $user_id ) ) { return false; } update_user_meta( $user_id, 'oneTarek_twitter', $_POST['oneTarek_twitter'] ); } #Developed By oneTarek , http://oneTarek.com function oneTarek_extra_user_profile_fields( $user ) { ?> <h3>Extra Custom Meta Fields</h3> <table class="form-table"> <tr> <th><label for="oneTarek_twitter">Twitter User Name</label></th> <td> <input type="text" id="oneTarek_twitter" name="oneTarek_twitter" size="20" value="<?php echo esc_attr( get_the_author_meta( 'oneTarek_twitter', $user->ID )); ?>"> <span class="description">Please enter your Twitter Account User name, eg: oneTarek</span> </td> </tr> </table> <?php }?>
I made a demo plugin with above codes. User my demo plugin to be clear about above codes.
Download My Demo Plugin to Add User Custom Field
- Add Custom field with wordpress user profile
- Add user custom field in pluign
- WordPress plugin to add extra meta fields for users
- How to user user custom fields
- Custom meta data for wordpress user
- How can I add new custom meta field for user profile
- Plugin tutorial to add user meta fields
- Create a new usermeta field for users
- Add Custom User Meta Fields
- How to Save Different Usermeta Fields
- Customize backend user profile
- Custom user registration by user meta
- Adding Custom Fields to WordPress User Profile
- Adding new meta key to wp_usermeta table in database
Permalink
i know designing. now i am learning web developing.. Hi can I become your friend? plz add me in ur gtalk
Permalink
Hey, You can become my friend on fb. Send me a fb friend request
Permalink
Is there a way to make a button that when pressed changes user profile meta to a pre-defined value.
Permalink
Your question is not clear to me. Where you want to show a button?
You can add HTML code for a button with onclick function, Write JavaScript code for what you want to do on click of this button.
Permalink
Doesn’t seem to work for the admin user.
Permalink
Im using this solution to add an extra field for user, but i want to use this extra field as an expiration date.
Something like: yyyy-mm-dd to eval expiration date
How can I add a datepicker to the date field to allow admin easily set exp. date?
Permalink
Just I need this. Thanks @Tarek vai
Permalink
Nice clear article. Appreciated!
Permalink
thanks
Permalink
This is really a good post, short and to the point. I just had a case I need to add twitter to my account and find here the answer. to make more easily to find you, I suggest you add the words twitter metadata to the title. (this is how I found you). as a lot of people need this info.
thank you oneTarek!
Permalink
Thanks. It help me.
Permalink
Thank you. This post was very helpful.
Permalink
Thank you.