WordPress-Christmas-2010-22

Adding Custom Fields to WordPress User Profile

The user profile of WordPress can be fairly easily adapted to add your own values​​. So you can add the necessary fields according to your requirements. Here is how you do it, we add a field for the address and the content will be stored in the database. Various hooks in WordPress make sure that you only have to worry about the fields.

The function to store the entries is fb_save_custom_user_profile_fields() and here it is important to check the rights of each active user, only if the rights are available, in this case for editing users (edit_user), then data may be stored.
The actual saving of data takes place via update_usermeta().

It can pass all kinds of fields of course, the input field is just an example. It is advisable to outsource the functions in a Plugin, an alternative is also the functions.php of the Theme, but is certainly not the best way.

Each function of the small sample was referred to two hooks. Because WordPress differentiate between editing of user profiles and update personal data. This is done using the constant IS_PROFILE_PAGE. On default this constant is on TRUE and so would the hooks show_user_profile and personal_options_update be enough to bring in new fields and save them. But this can vary depending on the installation and this way the admin cannot maintain these new fields. In particular, two more hooks are needed. Otherwise there might be many cases, where the admin has to maintain data which the user doesn't even see in his profile.

function fb_add_custom_user_profile_fields( $user ) {
?>
	<h3><?php _e('Extra Profile Information', 'your_textdomain'); ?></h3>
	
	<table class="form-table">
		<tr>
			<th>
				<label for="address"><?php _e('Address', 'your_textdomain'); ?>
			</label></th>
			<td>
				<input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br />
				<span class="description"><?php _e('Please enter your address.', 'your_textdomain'); ?></span>
			</td>
		</tr>
	</table>
<?php }

function fb_save_custom_user_profile_fields( $user_id ) {
	
	if ( !current_user_can( 'edit_user', $user_id ) )
		return FALSE;
	
	update_usermeta( $user_id, 'address', $_POST['address'] );
}

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

add_action( 'personal_options_update', 'fb_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'fb_save_custom_user_profile_fields' );

Comments are closed.

11 comments

  1. Casey

    Very helpful! What if I also wanted visitors to be able to search those custom fields from the front-end? Is that possible?

  2. dcolumbus

    @Casey, yes. It is entirely possible:

    http://codex.wordpress.org/Function_Reference/get_user_meta

    Within your theme, you can retrieve any part of the user profile this way. Whether you provide an AJAX search solution or just a standard form...

    The only catch is that you have to make sure that you CREATE that meta data (add_user_meta) before you can update or read it in. :)

  3. Michael Oeser

    Hi Frank,

    great thing but can you take a look at this: http://dl.dropbox.com/u/4814298/additional_fields.png

    I simply added a textarea and as you can see the sourcecode of the entire page is put there. This is what I entered into the functions file:

    http://dl.dropbox.com/u/4814298/fb_function1.php

    Any idea what went wrong?

    Vielen Dank im voraus.
    Gruß
    Michael

  4. Frank

    @Michael: you can also write on my german blog - all posts give it also in german, maybe its easier and faster; we dont check the comments not daily.
    But to your problem; you dont use the textarea wrong; the markup is wrong; see textarea

    right on your example.

    <textarea name="biofr" id="biofr" rows="5" cols="30" class="regular-text" /><?php echo esc_attr( get_the_author_meta( 'biofr', $user->ID ) ); ?></textarea><br />

  5. Profesor Yeow

    One question... do you have idea of how I can make to use this custom fields in all users BUT only edit per an admin and not per the users?

    I search over all page of internet and I do not found a solution for this problems.

    Thanks for the guide. And Regards!

  6. Michael Oeser

    @Frank: Oh my goodness...stupid me. I really should know basic HTML stuff ;-)

    Thanks for pointing my blind eyes into the right direction. Works now.

    Another question: Any idea how to move that stuff to another position on the profile page?

    PS: Beim Googlen bin ich halt auf den Artikel gestoßen, sonst hätte ich im DE Blog geschrieben ;-)

  7. Frank

    @Michael: i think you can not change the position to the core elements; but maybe its possible to change this via CSS or JS.

  8. eteich

    Awesome work, one fix:
    The field value will never show unless you change the value to:
    ID, 'address', ) ); ?>

  9. eteich

    this will only work properly if you use the get_usermeta function rather than the get_the_author_meta.

    This is because get_the_author_meta can only access a users meta fields when it is ran from a page that they are an author of. The Dashboard has no author privileges attached to it.

  10. Thomas

    Hi there,

    I'm trying to add several new profile fields not just one. Do I have to duplicate the whole function?

    Right now when copying the parts where 'address' appears and inserting it into the function below the 'address'.part I get a white screen...

    Cheers,
    Thomas.

One pingback

  1. Adventskalender am 22. Dezember « F-LOG-GE