Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - Saving Custom User Profile Fields

Tags:

php

wordpress

I am currently trying to add some custom user profile fields for my Wordpress users.

I have added the following code into my functions.php but for some reason the data entered is not saving...

//** CUSTOM USER META **//

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

function my_show_extra_profile_fields( $user ) { ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
        <tr>
            <th><label for="club">Club You Support</label></th>
            <td>
                <input type="text" name="club" id="club" value="<?php echo esc_attr( get_the_author_meta( 'club', $user->ID ) ); ?>" class="regular-text" /><br />
            </td>
        </tr>
    </table>
<?php }

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

   function my_save_extra_profile_fields( $user_id ) {
       update_usermeta( $user_id, 'club', sanitize_text_field( $_POST['club']) );
   } 

Any ideas as to why this data isn't sticking ?

like image 447
Phil Nind Avatar asked Apr 28 '15 06:04

Phil Nind


1 Answers

There is an easier and proper way to create new profile fields in Wordpress. Based on your code above, try dropping the code below on your functions.php file on your theme:

function my_show_extra_profile_fields {
    $user_contact_method['club'] = 'Club You Support';
    return $user_contact_method;
}
add_filter( 'user_contactmethods', 'my_show_extra_profile_fields' );

This will automatically create the new fields on your profile page and accordingly save them to the data base as custom fields (meta) for user.

You can display this info on your theme using the_author_meta('club');

like image 96
Gus Fune Avatar answered Nov 11 '22 20:11

Gus Fune