admin管理员组

文章数量:1122832

I am having multiple problems with my simple testing cron snippet.

First, it is not doing its job, which is updating theme option field.
Second, when checking in the Cron Events plugin dashboard, seems like it is not triggering every 3 minutes, as it should do.

Here is the code:

class RedirectionChecker {
    public function __construct() {
        add_filter('cron_schedules', [ $this, 'add_every_three_minutes' ]);
        add_action('init', [ $this, 'schedule_event' ]);
        add_action('redirection_checker_hook', [ $this, 'redirection_checker_func' ]);
    }

    public function add_every_three_minutes($schedules) {
        $schedules['every_three_minutes'] = [
            'interval' => 180,
            'display'  => __('Every 3 Minutes')
        ];
        return $schedules;
    }

    public function schedule_event() {
        if (!wp_next_scheduled('redirection_checker_hook')) {
            wp_schedule_event(time(), 'every_three_minutes', 'redirection_checker_hook');
        }
    }

    public function redirection_checker_func() {
        update_option('test_option', 'success');
    }
}

I am having multiple problems with my simple testing cron snippet.

First, it is not doing its job, which is updating theme option field.
Second, when checking in the Cron Events plugin dashboard, seems like it is not triggering every 3 minutes, as it should do.

Here is the code:

class RedirectionChecker {
    public function __construct() {
        add_filter('cron_schedules', [ $this, 'add_every_three_minutes' ]);
        add_action('init', [ $this, 'schedule_event' ]);
        add_action('redirection_checker_hook', [ $this, 'redirection_checker_func' ]);
    }

    public function add_every_three_minutes($schedules) {
        $schedules['every_three_minutes'] = [
            'interval' => 180,
            'display'  => __('Every 3 Minutes')
        ];
        return $schedules;
    }

    public function schedule_event() {
        if (!wp_next_scheduled('redirection_checker_hook')) {
            wp_schedule_event(time(), 'every_three_minutes', 'redirection_checker_hook');
        }
    }

    public function redirection_checker_func() {
        update_option('test_option', 'success');
    }
}

Share Improve this question asked Jun 4, 2024 at 9:52 Tahi ReuTahi Reu 3081 silver badge14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can add debugging statements to your code to verify if the cron job is being scheduled and executed correctly. like you can use error_log() to log messages to the PHP error log:

public function schedule_event() {
if (!wp_next_scheduled('redirection_checker_hook')) {
    error_log('Scheduling redirection_checker_hook');
    wp_schedule_event(time(), 'every_three_minutes', 'redirection_checker_hook');
}
}

public function redirection_checker_func() {
error_log('Running redirection_checker_func');
update_option('test_option', 'success');
}

本文标签: Having difficulties with WP cron