This section of research covers adding and editing usermeta fields to the various wp-admin forms.
"Registering" new usermeta fields must be accomplished by echoing HTML via different actions depending on the context.
In some contexts, you must echo a single table row, while in others you will need to echo an entire table, perhaps including a header as well.
Note: It is important to include the wp-admin css classes so the new fields blend in with the admin.
See
All user forms include 'user.php'
and use the edit_form()
function to perform user insert and updates. Yes, adding users also uses the edit_user()
function.
The insert_custom_user_meta
filter appends an arbitrary array immediately prior to running update_user_meta()
with each element in the array inside wp_insert_user()
(see wp-includes/user.php).
To attach a new meta we inform wp_insert_user()
about the previously unassigned $_POST
value.
add_filter( 'insert_custom_user_meta', 'attach_new_meta_field' );
public function attach_new_meta_field() {
if ( ! isset( $POST['new_meta_key'] ) ) {
return [];
}
return array( 'new_meta_key' => sanitize_text_field( $_POST['new_meta_key'] ) );
}