最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - how can I call a function when time expire

programmeradmin3浏览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);
        }
}}

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);
        }
}}
Share Improve this question asked Aug 18, 2020 at 15:03 freedom667freedom667 56 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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);
            }
        }
    }
}   

?>
发布评论

评论列表(0)

  1. 暂无评论