It’s easy. Just add function with your options to show_user_profile
and edit_user_profile
hooks, and save them on personal_options_update
and edit_user_profile_update
.
Like this for instance (you can add your own fields (text, number etc.))
/********* Additional User Content ***********/
add_action( 'show_user_profile', 'mytheme_extra_profile_fields' );
add_action( 'edit_user_profile', 'mytheme_extra_profile_fields' );
function mytheme_extra_profile_fields( $user ) { ?>
<?php $customData = get_user_meta( $user->ID, 'custom_user_fields', true ); ?>
<h3><?php esc_html_e('Custom option', 'mytheme'); ?></h3>
<table class="form-table">
<tr>
<th><?php esc_html_e('Custom option', 'mytheme'); ?></th>
<td>
<input type="checkbox" name="custom_user_fields_option" id="option" value="1" <?php if(isset($customData['checkbox'])){ checked($customData['checkbox'], 1); } ?>/>
<span class="description"><?php esc_html_e('Check for an option.', 'mytheme'); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'mytheme_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'mytheme_save_extra_profile_fields' );
function mytheme_save_extra_profile_fields( $user_id ) {
$userData = array();
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
if(!empty( $_POST['custom_user_fields_option'] )) {
$userData['checkbox'] = intval( $_POST['custom_user_fields_option'] );
}
if(!empty($userData)) {
update_user_meta( $user_id, 'custom_user_fields', $userData);
}
}
This will give you
In the single user page (profile.php
).