Skip to content

Instantly share code, notes, and snippets.

@sethrubenstein
Created August 17, 2015 15:06
Show Gist options
  • Save sethrubenstein/efd046fb3c3748ecfd5b to your computer and use it in GitHub Desktop.
Save sethrubenstein/efd046fb3c3748ecfd5b to your computer and use it in GitHub Desktop.
WP Missed Schedule CRON
<?php
/**
* Plugin Name: WP Missed Scheduled Check
* Description: Checks for missed scheduled posts once a day and force publishes them if they missed their scheduled publish time.
* Version: 1.0
* Author: Seth Rubenstein
* Author URI: http://sethrubenstein.info
* License: GPL2
*/
function srcron_missed_schedule_check() {
$db_host = DB_HOST;
$db_username = DB_USER;
$db_password = DB_PASSWORD;
$db_name = DB_NAME;
$db = new mysqli($db_host, $db_username, $db_password, $db_name);
if(! $db){
error("missed-schedule-check: db connection failed.");
exit();
}
header("HTTP/1.1 200 OK");
$stmt = $db->prepare("SELECT ID, post_date, now() FROM wp_posts WHERE post_status = 'future' AND post_date < now() ");
$stmt->execute();
$stmt->bind_result($id, $post_date, $now);
while($stmt->fetch()){
$difference = strtotime($now) - strtotime($post_date);
if($difference > 300){
$flag = "*";
} else {
$flag = "";
}
echo "Missed Schedule ID: " . $id . "\tPost Date: ". $post_date . "\tCurrent Time: " . $now . "\t" . $flag . "\n";
}
if($stmt){
$stmt->close();
}
if($db){
$db->close();
}
}
// Create Schedule
function srcron_cron_schedules() {
if ( ! wp_next_scheduled( 'srcron_daily_events' ) ) {
wp_schedule_event( time(), 'daily', 'srcron_daily_events');
}
}
add_action( 'wp', 'srcron_cron_schedules' );
// Run Schedule
function srcron_run_these_events_daily() {
srcron_missed_schedule_check();
}
add_action( 'srcron_daily_events', 'srcron_run_these_events_daily' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment