0

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);
        }
}}

1 Answer 1

0

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);
            }
        }
    }
}   

?>
11
  • this code works with my function?
    – freedom667
    Commented Aug 19, 2020 at 6:13
  • No- your function requires user profiles to fire. This feature is intended to replace your function and fire on a schedule (cron). I've included the action part of your function in the code. The only thing I can't know is if I'm getting "$date_to_compare" correctly. My original reply explains these issues. Commented Aug 19, 2020 at 12:47
  • $date_to_compare is entered date in user edit page
    – freedom667
    Commented Aug 19, 2020 at 13:29
  • I edited date_to compare to get_the_author_meta( 'year_of_birth', $user->ID ) then when I press "Run" it worked but schedule does not work. DISABLE_WP_CRON must be true or false? I wrote 'everyminute' instead of 'daily' for the Test
    – freedom667
    Commented Aug 19, 2020 at 14:24
  • Where did you press "Run"? You can install this plugin: wordpress.org/plugins/amr-cron-manager and verify that the cron event is scheduled. "custom_cron_event" Should be listed as a scheduled cron if you've activated the file as a plugin correctly. Commented Aug 19, 2020 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.