I have this code. When I save it, the user levels adding and gives time select input next to it. But when I give different time to levels, for example: Level 1: 2 weeks, Level 2: 9 Months
If saves only the last element, then all inputs display that value. How can I save different times? Where do I wrong?
//USER EDIT IN ADMIN DASHBOARD
add_action( 'show_user_profile', 'crf_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'crf_show_extra_profile_fields' );
function crf_show_extra_profile_fields( $user ) {
$user_levels = rua_get_user($user->ID)->get_level_ids();
$elements = array();
foreach ($user_levels as $user_level) {
$present_date = date("Y-m-d");
$end_time = get_the_author_meta( $user_level, $user->ID);
$effectiveDate = strtotime($end_time, strtotime(date("Y-m-d")));
$time = date("d/m/y", $effectiveDate);
$elements[] = get_the_title($user_level) .
' -> <select name="' .$user_level. '" id="time-selector" style="width:180px;">
<option value="default">Süre Seçiniz</option>
<option value="+14 day" '.selected( $end_time, "+14 day"). '>2 Hafta</option>
<option value="+9 months" '.selected( $end_time, "+9 months"). '>9 Ay</option>
<option value="+12 months" ' .selected( $end_time, "+12 months"). '>12 Ay</option>
<option value="+7 day" ' .selected( $end_time, "+7 day"). '>Uzat</option>
</select>' . $present_date . ' ' . $time . '<br><br>';
}
}
//SAVING DATE OF EACH LEVEL
add_action( 'personal_options_update', 'crf_update_profile_fields' );
add_action( 'edit_user_profile_update', 'crf_update_profile_fields' );
function crf_update_profile_fields( $user_id ) {
$user_levels = rua_get_user($user_id)->get_level_ids();
foreach ($user_levels as $user_level) {
update_user_meta( $user_id, $user_level, $_POST[ $user_level ] );
}
}
//DELETING LEVEL
function my_demo_cronjob_action () {
$present_date = date("Y-m-d");
$user = get_user_by('id',26);
$user_levels = rua_get_user($user)->get_level_ids(false, false, true);
foreach ($user_levels as $level) {
$end_time = get_the_author_meta( $level, $user->ID);
$effectiveDate = strtotime($end_time, strtotime(date("Y-m-d")));
$time = date("d/m/y", $effectiveDate);
if ($present_date > $time) {
rua_get_user($user)->remove_level($level);
}
}
}
add_action('my_demo_cronjob_action', 'my_demo_cronjob_action');
get_user_meta
rather thanget_the_author_meta
get_user_meta
instead,get_the_author_meta
is innapropriate for this use caseget_user_meta
can do things thatget_the_author_meta
cannot. Technicallyget_the_author_meta
shouldn't work here as the field parameter should not be able t accept arbitrary meta key values as there's a predefined whitelist, which means any answer will likely requireget_user_meta
, if not for that reason but because of its 3rd parameter. It is not possible to store the same meta key multiple times then retrieve them withget_the_author_meta
, but it is withget_user_meta
.