I have this function but the function name is “user_profile_update_errors”. so it does not work without press “Edit” button in user edit page. Which should I use function for it?
access levels are deleted if the date entered is greater than today’s date.
add_action( 'user_profile_update_errors', 'crf_user_profile_update_errors', 10, 3 );
function crf_user_profile_update_errors($errors, $update, $user ) {
$current_date = date("Y-m-d");
$date_to_compare = $_POST['year_of_birth'];
if ( ! $update ) {
return;
}
//Bugünün Tarihi// //Girilen Tarih//
if (strtotime($current_date > strtotime($date_to_compare)) ) {
$user_levels = rua_get_user($user)->get_level_ids(false, false, true);
foreach ($user_levels as $level) {
rua_get_user($user)->remove_level($level);
}
}}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The only way that I know of to perform an action daily (or on another regular interval) is to schedule a cron event. A sustainable way to do that is to write a little plugin that schedules/clear_schedules the cron when activated/deactivated. Here’s a plugin that you could use – I tested to ensure that it was installable and registered the cron event correctly. You will need to either place this in a file (such as custom_cron_event.php) and FTP it to your /plugins folder, or place it in a file and compress (ZIP) the file to upload it to your website through the /wp-admin interface.
Sadly, you can’t rely on a function that fires when user profiles are updated to retrieve user data anymore, because no profiles are being updated. Instead you’ll need to get the users your self and detect if a change is required.
<?php
/*
Plugin Name: Custom Plugin
Plugin URI:
Description: Adds function on cron
Author:
Version: 1.0
*/
/*
* When this plugin is activated, schedule/clear_schedule cron
*/
register_activation_hook(__FILE__, 'activate_custom_cron');
register_deactivation_hook(__FILE__, 'deactivate_custom_cron');
function activate_custom_cron(){
wp_schedule_event( time(), 'daily', 'do_custom_cron_event');
}
function deactivate_custom_cron(){
wp_clear_scheduled_hook('do_custom_cron_event');
}
add_action( 'do_custom_cron_event', 'custom_cron_event' );
function custom_cron_event() {
/*
* This is where your function goes
*/
$current_date = date("Y-m-d");
$users = get_users();
foreach( $users as $user){
$date_to_compare = get_user_meta($user->id, 'year_of_birth');
if (strtotime($current_date) > strtotime($date_to_compare) ) {
$user_levels = rua_get_user($user)->get_level_ids(false, false, true);
foreach ($user_levels as $level) {
rua_get_user($user)->remove_level($level);
}
}
}
}
?>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0