0

I'm using the new(ish) filter add_action( 'user_new_form', 'funcy', 9 ); to add custom fields to Create A New User page. I'm then using

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

hooks to save my meta on other edit user pages. I've confirmed that when creating a new user WP does not hook into the above. I've tried looking through

  1. /wp-admin/user-new.php
  2. /wp-adin/user-edit.php

but nothing stuck out as to how it was saving the new user info. The core of the question is, what can I hook into to save user_meta from the user_new_form?

2 Answers 2

1

For this, you have to use user_register hook defined on wp-includes/users.php line 1759.

  • personal_options_update hook is called, when an user updating his own profile.
  • edit_user_profile_update is called, when an administrator updating other user profile.
  • nothing is called, when an administrator creating a new user.

On user-new.php page, new user are created using the edit_user function, function ref: (wp-admin/includes/users.php line 30)

So, the only option to use user_register hook.

0
0

It adds field in the New User and Edit User Screens. Don't forget to include this in your functions.php file.

<?php

class UserFieldsClass
{
    public function __construct()
    {
        // This will create a new area in the user edit screen and the add new user screen.
        add_action( 'user_new_form', array( $this, 'show_extra_profile_fields' ) );
        add_action( 'show_user_profile', array( $this, 'show_extra_profile_fields' ) );
        add_action( 'edit_user_profile', array( $this, 'show_extra_profile_fields' ) );

        add_action( 'personal_options_update', array($this, 'save_extra_profile_fields') );
        add_action( 'edit_user_profile_update', array($this, 'save_extra_profile_fields') );
        add_action( 'user_register', array( $this, 'save_extra_profile_fields' ) );
    }

    function show_extra_profile_fields($user)
    { ?>
        <h3>More Information</h3>
        <table class="form-table">
            <tr>
                <th><label for="user_skype">Skype:</label></th>
                <td>
                    <input type="text" name="user_skype" id="user_skype"
                           value="<?php echo esc_attr(get_the_author_meta('user_skype', $user->ID)); ?>"
                           class="regular-text"/><br/>
                    <span class="description">Please enter your Skype username.</span>
                </td>
            </tr>
        </table>
    <?php }

    function save_extra_profile_fields( $user_id ) {
        if (!current_user_can('manage_options'))
            return false;

        update_user_meta($user_id, 'user_skype', $_POST['user_skype']);
    }
}

$userFieldsClass = new UserFieldsClass();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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