0

I have a WordPress Multisite build and I'm trying to add a custom field to the admin user registration form but the metadata isn't saving in the initial form: after I create the user, I can go to the new profile page and the custom field will save.

Here is my function to add the custom field, which does display on the registration page (/wp-admin/user-new.php).

/**
 * Add custom fields to WordPress account registration
 *
 * @param $user
 */
function add_rm_tenant_info( $user ) { ?>
    <h3>Rent Manager Tenant Information</h3>
    <table class="form-table">
        <tr>
            <th><label for="accountNumber">Account Number</label></th>
            <td>
                <input type="number" name="accountNumber" id="accountNumber" value="<?php echo esc_attr( get_the_author_meta( 'accountNumber', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description">This number is used to connect the tenant's information from Rent Manager to their WordPress profile.</span>
            </td>
        </tr>
    </table>
    <?php
}
add_action( 'show_user_profile', 'add_rm_tenant_info' );
add_action( 'edit_user_profile', 'add_rm_tenant_info' );
add_action( "user_new_form", "add_rm_tenant_info" );

And here is my function to save the field. It doesn't seem to work on the registration page, but will save after I create the user and edit the profile.

/**
 * Saves custom fields
 *
 * @param $user_id
 * @return bool
 */
function save_rm_tenant_info( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;
    update_user_meta( $user_id, 'accountNumber', $_POST['accountNumber'] );
}
add_action( 'personal_options_update', 'save_rm_tenant_info' );
add_action( 'edit_user_profile_update', 'save_rm_tenant_info' );
add_action( 'user_register', 'save_rm_tenant_info' );
add_action( 'profile_update', 'save_rm_tenant_info' );
1

You need to use the show_user_profile, edit_user_profile, personal_options_update, and edit_user_profile_update hooks.

You can use the following code format adding additional fields in User section.

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

<table class="form-table">
<tr>
    <th><label for="address"><?php _e("Address"); ?></label></th>
    <td>
        <input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br />
    </td>
</tr>
</table>
<?php } ?>

For Saving the usemeta's in database,

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, 'address', $_POST['address'] );
}

There are also several blog posts available on the subject that might be helpful:

link 1

link 2

Hope this will helpful for you.

  • I have those added already, see my last 4 lines of code. – Keith Petrillo Nov 1 '17 at 12:19
  • yes, but why you added the extra function calls? like:- add_action( 'user_register', 'save_rm_tenant_info' ); add_action( 'profile_update', 'save_rm_tenant_info' ); – Sunil Dora Nov 1 '17 at 12:48
  • Because those are the hooks for editing profile pages. – Keith Petrillo Nov 1 '17 at 12:48
  • but you don't need it,I used the same example it's working for me perfectly. – Sunil Dora Nov 1 '17 at 12:50
  • Are you on a Multisite platform? Which user resigtartion page are you on? – Keith Petrillo Nov 1 '17 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.