xxxxxxxxxx
// ADD USER META FIELD on WP Admin PROFILE EDIT (BIRTHDAY)
// Show and save on edit
function tm_additional_profile_fields( $user ) {
$months = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );
$default = array( 'day' => 1, 'month' => 'Jnuary', 'year' => 1950, );
$birth_date = wp_parse_args( get_the_author_meta( 'birth_date', $user->ID ), $default );
?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="birth-date-day">Birth date</label></th>
<td>
<select id="birth-date-day" name="birth_date[day]"><?php
for ( $i = 1; $i <= 31; $i++ ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $i, selected( $birth_date['day'], $i, false ) );
}
?></select>
<select id="birth-date-month" name="birth_date[month]"><?php
foreach ( $months as $month ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $month, selected( $birth_date['month'], $month, false ) );
}
?></select>
<select id="birth-date-year" name="birth_date[year]"><?php
for ( $i = 1950; $i <= 2015; $i++ ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $i, selected( $birth_date['year'], $i, false ) );
}
?></select>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'tm_additional_profile_fields' );
add_action( 'edit_user_profile', 'tm_additional_profile_fields' );
function tm_save_profile_fields( $user_id ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
if ( empty( $_POST['birth_date'] ) ) {
return false;
}
update_usermeta( $user_id, 'birth_date', $_POST['birth_date'] );
}
add_action( 'personal_options_update', 'tm_save_profile_fields' );
add_action( 'edit_user_profile_update', 'tm_save_profile_fields' );
xxxxxxxxxx
<?php
// Add to a WordPress User some metaData (so you can control it somewhere else)
$user_id = 1;
$metaData = 'subscribed';
add_user_meta( $user_id, '_subscribedService1', $metaData);
?>
xxxxxxxxxx
<?php
/**
* The field on the editing screens.
*
* @param $user WP_User user object
*/
function wporg_usermeta_form_field_birthday( $user )
{
?>
<h3>It's Your Birthday</h3>
<table class="form-table">
<tr>
<th>
<label for="birthday">Birthday</label>
</th>
<td>
<input type="date"
class="regular-text ltr"
id="birthday"
name="birthday"
value="<?= esc_attr( get_user_meta( $user->ID, 'birthday', 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>
<p class="description">
Please enter your birthday date.
</p>
</td>
</tr>
</table>
<?php
}
/**
* The save action.
*
* @param $user_id int the ID of the current user.
*
* @return bool Meta ID if the key didn't exist, true on successful update, false on failure.
*/
function wporg_usermeta_form_field_birthday_update( $user_id )
{
// check that the current user have the capability to edit the $user_id
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
// create/update user meta for the $user_id
return update_user_meta(
$user_id,
'birthday',
$_POST['birthday']
);
}
// Add the field to user's own profile editing screen.
add_action(
'show_user_profile',
'wporg_usermeta_form_field_birthday'
);
// Add the field to user profile editing screen.
add_action(
'edit_user_profile',
'wporg_usermeta_form_field_birthday'
);
// Add the save action to user's own profile editing screen update.
add_action(
'personal_options_update',
'wporg_usermeta_form_field_birthday_update'
);
// Add the save action to user profile editing screen update.
add_action(
'edit_user_profile_update',
'wporg_usermeta_form_field_birthday_update'
);