I’m working on a custom event scheduler for my website that allows people to easily go in and set the article to post in the future. The reason I’m overriding the normal WordPress functionality is because I wanted to make it super easy to choose the date, since the sermons should only be posted on a Sunday. So I have a custom dropdown calendar that only allows you to select the Sunday’s of a month.
Anyways my problem is that all my posts give an error of “Missed Schedule” in the admin panel. I’m able to get around this by using the “post_status” => “future” to list posts on the page, but this is kinda “hacky”…
Why would this code be giving me that “Missed Schedule” error?
function cfc_reset_sermondate( $data ) {
if($data['post_type'] == 'sermon_post') {
if($_POST['cfc_sermon_date']) {
$date = $_POST['cfc_sermon_date'];
// $date = DateTime::createFromFormat('D - M j, Y', $date);
// $date = $date->format('Y-m-d');
$date = createFromFormat($date);
$postDate = strtotime($date);
$data['post_date'] = $date;
$todaysDate = strtotime( date( 'Y-m-d' ) );
if ( $postDate > $todaysDate ) {
$data['post_status'] = 'future';
}
}
}
return $data;
}
add_filter( 'wp_insert_post_data', 'cfc_reset_sermondate', '99', 1);
Here’s the createFromFormat function:
function createFromFormat($date_ugly) {
$schedule = 'Sunday - Sep 15, 2000';
// %Y, %m and %d correspond to date()'s Y m and d.
// %I corresponds to H, %M to i and %p to a
$ugly = strptime($date_ugly, '%A - %b %e, %Y');
$ymd = sprintf(
// This is a format string that takes six total decimal
// arguments, then left-pads them with zeros to either
// 4 or 2 characters, as needed
'%04d-%02d-%02d',
$ugly['tm_year'] + 1900, // This will be "111", so we need to add 1900.
$ugly['tm_mon'] + 1, // This will be the month minus one, so we add one.
$ugly['tm_mday'],
$ugly['tm_hour'],
$ugly['tm_min'],
$ugly['tm_sec']
);
$new_schedule = new DateTime($ymd);
return $new_schedule->format('Y-m-d');
}
WordPress has this functionality: by setting the publication date to the future in the WordPress ‘publish’ box, the post will be given ‘future’ status and will be scheduled to publish for that date.
My guess is that the problem arises because you haven’t set the GMT time – which is what the WordPress uses to schedule the publications. Or otherwise you’ve hooked in after the scheduling has been done.
The ‘easy’ way (but leaves you with two ‘publish date meatboxes’ – which from a UI point of view is not great).
Keep in mind that WP expects the date in the
Y-m-d H:i:s
formatThis should work, but is not tested!
Alternative
Alternatively you can de-regester the publish metabox, and re-register it with exactly the same mark-up (but with some ID tags changed so WP’s javascript ignores it, and then apply your own javascript to it). From a UI point of view this much better, but a bit more involved.
The first part is outlined in this post for another metabox.