How to add new custom field in default add user form through plugin

admin2025-06-02  1

I am new in wordpress , started plugin development in wordpress a week ago . I have to add the new custom field in default add user form , is it possible . Then i if write wp_get_current_user() , is it possible to get the data which is added in the new custom field ? Any body please help . Thank you in advance .

I am new in wordpress , started plugin development in wordpress a week ago . I have to add the new custom field in default add user form , is it possible . Then i if write wp_get_current_user() , is it possible to get the data which is added in the new custom field ? Any body please help . Thank you in advance .

Share Improve this question edited Mar 1, 2019 at 14:39 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Mar 1, 2019 at 13:58 Mohammad HamdanMohammad Hamdan 32 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

It is possible. For that we will use 4 hooks (actions): show_user_profile, edit_user_profile, personal_options_update, edit_user_profile_update.

Here is the example how to add extra field to user in WordPress.

Adding user info fields function

add_action( 'show_user_profile', 'extra_user_profile_fields', 10, 1 );
add_action( 'edit_user_profile', 'extra_user_profile_fields', 10, 1 );

function extra_user_profile_fields( $user ) { ?>
    <h3><?php _e("Extra profile information", "blank"); ?></h3>

    <table class="form-table">
    <tr>
        <th><label for="user_img_path_image"><?php _e("Image path"); ?></label></th>
        <td>
            <input type="text" name="user_img_path_image" id="user_img_path_image" value="<?php echo esc_attr( get_the_author_meta( 'user_img_path_image', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e("Path to country image."); ?></span>
        </td>
    </tr>
    <tr>
        <th><label for="user_country_field"><?php _e("Country field"); ?></label></th>
        <td>
            <input type="text" name="user_country_field" id="user_country_field" value="<?php echo esc_attr( get_the_author_meta( 'user_country_field', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e(""); ?></span>
        </td>
    </tr>

    </table>
<?php }

Here is the extra code for save and edit function

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

function save_extra_user_profile_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) ) { 
        return false; 
    }
    update_user_meta( $user_id, 'user_img_path_image', $_POST['user_img_path_image'] );
    update_user_meta( $user_id, 'user_country_field', $_POST['user_country_field'] );

}
// end - Add user info fields

Just copy and paste this code in your plugin or function.php and you must see new 2 fields in Wordpress backend user editing/adding page.

For extracting your date in frontend use this code:

$user_facebook_id_acc = get_the_author_meta( 'user_facebook_id_acc', $current_user_data->ID );

This is very possible. In WordPress, there is a concept of Filters and Actions, collectively known as "hooks". Hooks allow you to "hook into" WordPress to change values (filters) or to execute your own functions (actions).

For the WordPress user page, you should look into the following hooks:

  • show_user_profile - Fires for the current user after the "About Yourself" section
  • edit_user_profile - Fires when editing another user's profile after the "About the User" section

These two actions fire when viewing the page. Here, you can add your own code to display new fields, e.g.

add_action( 'personal_options_update', function( $user ) {
    $my_field_value = $user->my_field_key; // 'my_field_key' is the user meta field key in wp_usermeta
    // You can also use
    $my_field_value = get_user_meta( $user->ID, 'my_field_key', true );
?>
<input type="text" name="my_field_key" value="<?php echo esc_attr( $my_field_value ); ?>"/>
<?php
});

Then, you can use the following actions to update the user: - personal_options_update - Fires when you update your own profile page. - edit_user_profile_update - Fires when you update another user's profile.

add_action( 'personal_options_update', function( $user_id ) {
    // You should escape and verify the input.
    $my_value = filter_input( INPUT_GET, 'my_field_key', FITLER_SANITIZE_STRING );

    if ( ! $my_value ) {
        delete_user_meta( $user_id, 'my_field_key' );
        return;
    }

    update_user_meta( $user_id, 'my_field_key', $my_value );
} );

This could use some better HTML to fit the page, and should be modified for your usage, but hopefully this gives you an idea of where to go. Take a look at /wp-admin/user-edit.php for more details.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748850356a314243.html

最新回复(0)