Wordpress - saving user profile user fields

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

I added the following code to my functions.php, but for some reason, the entered data is not saved ...

//** 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 is not sticking?

+4
source share
1 answer

There is an easier and more correct way to create new profile fields in Wordpress. Based on your code above, try dropping the code below in the file functions.phpfor your topic:

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

, , () .

, the_author_meta('club');

+1

Source: https://habr.com/ru/post/1585044/


All Articles