I have added to admin users a custom meta field using the following code:``
function wporg_usermeta_form_field_birthday( $user )
{
?>
<table class="form-table" id="table-form-dob" >
<tr>
<th><h3 style="margin: 0">Extra Meta Fields</h3></th>
</tr>
<tr>
<th>
<label for="user_dob">Birthday</label>
</th>
<td>
<input type="date"
class="regular-text ltr"
id="user_dob"
name="user_dob"
value="<?= esc_attr( get_user_meta( $user->ID, 'user_dob', true ) ) ?>"
title="Please use YYYY-MM-DD as the date format."
pattern="(19[0-9][0-9]|20[0-9][0-9])-(1[0-2]|0[1-9])-(3[01]|[21][0-9]|0[1-9])"
required>
</td>
</tr>
</table>
<script>
jQuery(function($){
jQuery('#table-form-dob tr').insertAfter(jQuery('#display_name').parentsUntil('tr').parent());
});
</script>
<?php
}
function wporg_usermeta_form_field_birthday_update( $user_id )
{
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
return update_user_meta(
$user_id,
'user_dob',
$_POST['user_dob']
);
}
add_action(
'show_user_profile',
'wporg_usermeta_form_field_birthday'
);
add_action(
'edit_user_profile',
'wporg_usermeta_form_field_birthday'
);
add_action(
'personal_options_update',
'wporg_usermeta_form_field_birthday_update'
);
add_action(
'edit_user_profile_update',
'wporg_usermeta_form_field_birthday_update'
);
register_meta('user', 'user_dob', array(
"type" => "string",
"show_in_rest" => true // this is the key part
));
I want to add this same field In woocommerce checkout page, so when the user get registered in woocommerce checkout page, we should be able to see this "Birthday" field (user_dob
) in admin user profile/ edit section.
also, I am accessing user meta in rest API currently its showing meta in rest API after user saver values in check it should value in wp rest API.
How can I add this?