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 ) {
?>
	



Posted

in

by

Comments

11 responses to “Adding Custom Fields to WordPress User Profile”

  1. […] WPEngineer erzählt uns Frank Bültge, wie man Felder zum Nutzerprofil bei WordPress hinzufügen kann. Im TYPO3-Adventskalender erzählt uns Sven Wolfermann heute etwas über […]

  2. Casey Avatar

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

  3. dcolumbus Avatar

    @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. 🙂

  4. Michael Oeser Avatar

    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

  5. Frank Avatar

    @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 />
  6. Profesor Yeow Avatar

    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!

  7. Michael Oeser Avatar

    @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 😉

  8. Frank Avatar

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

  9. eteich Avatar

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

  10. eteich Avatar

    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.

  11. Thomas Avatar
    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.