简体   繁体   中英

Adding a custom field to the user edit page within an existing section

I am trying to add an extra field to the user-edit.php page on WordPress.

I want to add it to the "About this User/You" section right above the "Biographical Info" field. I had already edited the file to add another but for some reason, it will not save the information I enter into it.

<table class="form-table">
<tr class="user-job-title-wrap">
    <th><label for="job_title"><?php _e('Job Title'); ?></label></th>
    <td><input type="text" name="job_title" id="job_title" value="<?php echo esc_attr( $profileuser->job_title ) ?>" class="regular-text ltr" />
</td>
</tr>
<tr class="user-description-wrap">
    <th><label for="description"><?php _e('Biographical Info'); ?></label></th>
    <td><textarea name="description" id="description" rows="5" cols="30"><?php echo $profileuser->description; // textarea_escaped ?></textarea>
    <p class="description"><?php _e('Share a little biographical information to fill out your profile. This may be shown publicly.'); ?></p></td>
</tr>
...

It does add the correct meta_key to the wp_usermeta db but the meta_value stays blank. If I manually enter the meta_value in the db and reload the user edit page, the value is correctly loaded into the input field, but any changes made to this field are not recorded to the db.

Here are the wordpress hooks to add fields and saving updated fields data:

// ADDING CUSTOM FIELDS TO INDIVIDUAL USER SETTINGS PAGE AND TO USER LIST
add_action( 'show_user_profile', 'add_extra_user_fields' );
add_action( 'edit_user_profile', 'add_extra_user_fields' );
function add_extra_user_fields( $user )
{
    ?>
        <h3><?php _e("My TITLE", "my_theme_domain"); ?></h3>
        <table class="form-table">
            <tr class="user-job-title-wrap">
                <th><label for="job_title"><?php _e('Job Title'); ?></label></th>
                <td><input type="text" name="job_title" id="job_title" value="<?php echo esc_attr( $profileuser->job_title ) ?>" class="regular-text ltr" />
            </td>
            </tr>
            <tr class="user-description-wrap">
                <th><label for="description"><?php _e('Biographical Info'); ?></label></th>
                <td><textarea name="description" id="description" rows="5" cols="30"><?php echo $profileuser->description; // textarea_escaped ?></textarea>
                <p class="description"><?php _e('Share a little biographical information to fill out your profile. This may be shown publicly.'); ?></p></td>
            </tr>
        </table>
    <?php
}

--(Updated)-- You can use only save_extra_user_fields below to solve your issue (…saving information…), and keep your code if you like.

// Saving Updated fields data
add_action( 'personal_options_update', 'save_extra_user_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_fields' );
function save_extra_user_fields( $user_id )
{
    update_user_meta( $user_id, 'job_title', sanitize_text_field( $_POST['job_title'] ) );
    update_user_meta( $user_id, 'description', sanitize_text_field( $_POST['description'] ) );
}

This code goes on function.php file of your active child theme or theme…

Using your Custom User Fields data with php:

  • Returning data: get_the_author_meta( 'your_field_slug', $userID );
  • Displaying data (echo the data): the_author_meta( 'your_field_slug', $userID );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM