这个edit_user_created_user
在通过添加用户时适用user-new.php
:
add_action( \'edit_user_created_user\', \'wpse_edit_user_created_user\', 10, 2 ); //for user-new.php page new user addition
function wpse_edit_user_created_user( $user_id, $notify ) {
$meta = get_user_meta( $user_id, \'my_field\', true );
}
为了完整性,我使用以下代码来测试用户元数据,这是我从
this answer.
/**
* Declaring the form fields
*/
function show_my_fields( $user ) {
$fetched_field = get_user_meta( $user->ID, \'my_field\', true ); ?>
<tr class="form-field">
<th scope="row"><label for="my-field"><?php _e(\'Field Name\') ?> </label></th>
<td><input name="my_field" type="text" id="my-field" value="<?php echo esc_attr($fetched_field); ?>" /></td>
</tr>
<?php
}
add_action( \'show_user_profile\', \'show_my_fields\' ); //show in my profile.php page
add_action( \'edit_user_profile\', \'show_my_fields\' ); //show in my profile.php page
//add_action( \'user_new_form_tag\', \'show_my_fields\' ); //to add the fields before the user-new.php form
add_action( \'user_new_form\', \'show_my_fields\' ); //to add the fields after the user-new.php form
/**
* Saving my form fields
*/
function save_my_form_fields( $user_id ) {
update_user_meta( $user_id, \'my_field\', $_POST[\'my_field\'] );
}
add_action( \'personal_options_update\', \'save_my_form_fields\' ); //for profile page update
add_action( \'edit_user_profile_update\', \'save_my_form_fields\' ); //for profile page update
add_action( \'user_register\', \'save_my_form_fields\' ); //for user-new.php page new user addition