6
votes

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 ?

1
I copy-pasted this code, and worked for me perfectly. My add_action looks like this add_action( 'hook', 'function_name', 10, 1 );, but i dont think it makes any difference. - Pine
So strange! Could it be because I am using it on a staging domain? Maybe it does not have access to the database of the real site :/ - Phil Nind
I dropped this directly into my functions.php file and it worked for me as well. Could there be another plugin taking over that action on your install? - Dylan Hildenbrand

1 Answers

2
votes

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');