Custom Scheduled recurring task issue in WordPress

I defined 3 types of custom schedule tasks in the following script below. First one same day, which will be executed from current time to specific hours later. 2nd one "later" which will be executed from current date to specific date like 2 or 3 days later. Last one is "recurring" which will be executed every 2 or 3 days later. These days are defined from users. Within following script first 2 schedule output is showing Admin Notice section. But the "recurring" schedule task, doesn't show in Admin Notice section. I can't find out any issue. Where is the problem? How to fix this issue? The code I use: add_filter('cron_schedules', function($schedules) { $recurdate = get_option('recurdate'); $interval_seconds = $recurdate * 24 * 60 * 60; // Convert days to seconds $schedules['custom_recurring'] = array( 'interval' => $interval_seconds, 'display' => sprintf(__('Every %d days', 'ai-blog'), $recurdate) ); return $schedules; }); // Register activation hook register_activation_hook(__FILE__, 'slf_schedule_posts_activation'); function slf_schedule_posts_activation() { $schedule = get_option('schedule'); $sameschedule = get_option('same_schedule'); // Schedule for same-day posts if ($schedule === 'sameday') { if ($sameschedule === 'later_same_day') { $hours_delay = (int) get_option('hoursInput'); $minutes_delay = (int) get_option('minutesInput'); $delay_in_seconds = ($hours_delay * 3600) + ($minutes_delay * 60); $current_time = time(); $next_scheduled_time = $current_time + $delay_in_seconds; if (!wp_next_scheduled('slf_publish_later_same_day')) { wp_schedule_single_event($next_scheduled_time, 'slf_publish_later_same_day'); } } } // Schedule for later posts elseif ($schedule === 'later') { $later_days = get_option('laterdate'); $oclock = get_option('oclock'); if ($later_days !== false && $oclock !== false) { $later_date = date('Y-m-d', strtotime("+$later_days days")); $schedule_time = "$later_date $oclock:00"; $time_to_schedule = strtotime($schedule_time); if (!wp_next_scheduled('slf_publish_posts_event')) { wp_schedule_event($time_to_schedule, 'daily', 'slf_publish_posts_event'); } } } // Schedule for recurring posts elseif ($schedule === 'recurring') { $recurdate = get_option('recurdate'); $rcuroclock = get_option('rcuroclock'); //if ($recurdate !== false && $rcuroclock !== false) { $recur_date = date('Y-m-d', strtotime("+$recurdate days")); $schedule_time = "$recur_date $rcuroclock:00"; $time_to_schedule = strtotime($schedule_time); if (!wp_next_scheduled('slf_publish_recurring_posts')) { wp_schedule_event($time_to_schedule, 'custom_recurring', 'slf_publish_recurring_posts'); error_log("Scheduled recurring post event at " . date('Y-m-d H:i:s', $time_to_schedule)); } else { error_log("Recurring event already scheduled."); } //} } } // Register deactivation hook to clear scheduled events register_deactivation_hook(__FILE__, 'slf_schedule_posts_deactivation'); function slf_schedule_posts_deactivation() { wp_clear_scheduled_hook('slf_publish_later_same_day'); wp_clear_scheduled_hook('slf_publish_posts_event'); wp_clear_scheduled_hook('slf_publish_recurring_posts'); error_log("Deactivation hook: All scheduled events cleared."); } // Display admin notice based on the schedule function display_schedule_notice($event_name) { global $wpdb; $next_schedule = wp_next_scheduled($event_name); $table_name = $wpdb->prefix . 'slf_generated_title'; $query = "SELECT generate_title FROM $table_name ORDER BY id DESC LIMIT 1"; $result = $wpdb->get_var($query); $user_timezone = get_user_meta(get_current_user_id(), 'timezone_string', true); if (empty($user_timezone)) { $user_timezone = 'UTC'; } if ($next_schedule) { $date_in_utc = new DateTime('@' . $next_schedule); $date_in_utc->setTimezone(new DateTimeZone('UTC')); $schedule = get_option('schedule'); $sameschedule = get_option('same_schedule'); $formatted_publish_time = ''; if ($schedule === 'sameday') { if ($sameschedule === 'later_same_day') { $hours = (int) get_option('hoursInput'); $minutes = (int) get_option('minutesInput'); $publish_time = $date_in_utc->setTime($hours, $minutes); $date_in_user_timezone = $publish_time->setTimezone(new DateTimeZone($user_timezone)); $formatted_publish_time = $date_in_user_timezone->format('Y-m-d g:i a'); } } elseif ($schedule === 'later') { $laterdate = get_option('laterdate'); $oclock = get_option('oclock'); $publish_time = $date_in_utc; $publish_time->modify('+' . intval($laterdate) . ' days'); $time_parts = date_parse($oclock); $publish_hour = $time_parts['hour']; $publish_minute = $time_parts['minute']; if (strpos(strtolower($oclock), 'pm') !== false && $publish_hour < 12) { $publish_hour += 12; } elseif (strpos(strtolower($oclock), 'am') !== false && $publish_hour == 12) { $publish_hour = 0; } $publish_time->setTime($publish_hour, $publish_minute); $date_in_user_timezone = $publish_time->setTimezone(new DateTimeZone($user_timezone)); $formatted_publish_time = $date_in_user_timezone->format('Y-m-d g:i a'); } elseif ($schedule === 'recurring') { $recurdate = get_option('recurdate'); $rcuroclock = get_option('rcuroclock'); $publish_time = $date_in_utc; $publish_time->modify('+' . intval($recurdate) . ' days'); $time_parts = date_parse($rcuroclock); $publish_hour = $time_parts['hour']; $publish_minute = $time_parts['minute']; if (strpos(strtolower($rcuroclock), 'pm') !== false && $publish_hour < 12) { $publish_hour += 12; } elseif (strpos(strtolower($rcuroclock), 'am') !== false && $publish_hour == 12) { $publish_hour = 0; } $publish_time->setTime($publish_hour, $publish_minute); $date_in_user_timezone = $publish_time->setTimezone(new DateTimeZone($user_timezone)); $formatted_publish_time = $date_in_user_timezone->format('Y-m-d g:i a'); var_dump($formatted_publish_time); } $next_title = !empty($result) ? esc_html($result) : __('No more titles available', 'ai-blog'); echo '<div class="notice notice-success is-dismissible"> <p>' . sprintf( __('The schedule is set. Next post will be published on: <strong>%s</strong>. Next title: <strong>%s</strong>.', 'ai-blog'), esc_html($formatted_publish_time), $next_title ) . '</p> </div>'; } else { echo '<div class="notice notice-warning is-dismissible"> <p>' . __('No scheduled event found.', 'ai-blog') . '</p> </div>'; error_log("No scheduled event found for event_name = $event_name"); } } // Add admin notice to display schedule information add_action('admin_notices', 'slf_schedule_admin_notices'); function slf_schedule_admin_notices() { $schedule = get_option('schedule'); $sameschedule = get_option('same_schedule'); error_log("Admin notice: schedule_post_setup = $schedule"); if ($schedule === 'sameday') { if ($sameschedule === 'later_same_day') { display_schedule_notice('slf_publish_later_same_day'); } } elseif ($schedule === 'later') { display_schedule_notice('slf_publish_posts_event'); } elseif ($schedule === 'recurring') { display_schedule_notice('slf_publish_recurring_posts'); } }

Comment (0)

You’ll be in good company