Make WordPress Core

Opened 7 weeks ago

Last modified 7 weeks ago

#60657 new defect (bug)

wp_schedule_event problem

Reported by: odoremieux's profile odoremieux Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.4.3
Component: Cron API Keywords:
Focuses: Cc:

Description (last modified by dd32)

In some cases there are some problems with wp_schedule_event. It schedule the event, if we use wp_next_scheduled, it gives a timestamp, but using WP Crontrol, it doesn't appear there.
Below some code to reproduce it.
If you click on the button, it will schedule 500 events. Wait a bit, that 100 or so are processed (look at the logs or use WP Control to check). Then click again. The logs show that they should have been schedules, there is a timestamp and the transient is created. But it's not really scheduled, it doesn't appear with WP Control, and the transient is not deleted (for the 2nd round)

if (!class_exists("NSM_Test_Cron")) {

    class NSM_Test_Cron {

        function __construct() {
            add_action('admin_menu', array($this, 'my_custom_button_menu'));
            add_filter('cron_schedules', array($this, 'cron_schedules'));
            add_action('nsm_test_cron', array($this, 'nsm_test_cron'));

        public function cron_schedules($schedules) {
            if (!isset($schedules['nsm_60_seconds'])) {
                $schedules['nsm_60_seconds'] = array('interval' => 60, 'display' => '60 Seconds');
            return $schedules;

        function my_custom_button_menu() {            
            add_menu_page('My Custom Button', 'Custom Button', 'manage_options', 'my-custom-admin-button', array($this, 'my_custom_button_page'));

        function my_custom_button_page() {
            if (isset($_POST['action']) && $_POST['action'] == 'myaction') {
                error_log('my_custom_button_page action');
                for ($index = 0; $index < 500; $index++) {
                    $args = array(array('email' => '', 'user_id' => '1234', 'tags' => $index));
                    if (!wp_next_scheduled('nsm_test_cron', $args)) {
                        $error = wp_schedule_event(time(), 'nsm_60_seconds', 'nsm_test_cron', $args, true);
                        set_transient( 'doing_cron_' . $index, $index );
                        error_log('error -> ' . print_r($error, true));
                        error_log("wp_next_scheduled ($index)-> " . print_r(wp_next_scheduled('nsm_test_cron', $args), true));
            <div class="wrap">
                <h2>My Custom Button</h2>
                <form action="" method="post">
                    <input type="hidden" name="action" value="submit_my_custom_form">
                    <?php wp_nonce_field('my_custom_form_nonce'); ?>
                    <input type="text" name="my_custom_data" placeholder="Enter some data">
                    <input type="hidden" name="action" value="myaction">
                    <input type="submit" class="button button-primary" value="Submit">
        public function nsm_test_cron($args) {
            $error = wp_clear_scheduled_hook('nsm_test_cron', array($args));
            error_log('nsm_test_cron -> ' . $args['tags']);
            delete_transient( 'doing_cron_' . $args['tags']);

new NSM_Test_Cron();

Change History (1)

#1 @dd32
7 weeks ago

  • Component changed from General to Cron API
  • Description modified (diff)
Note: See TracTickets for help on using tickets.