Skip to content

Instantly share code, notes, and snippets.

@dmhendricks
Last active December 21, 2023 04:10
Show Gist options
  • Save dmhendricks/b752f93470a3e7f186f8428665d79e19 to your computer and use it in GitHub Desktop.
Save dmhendricks/b752f93470a3e7f186f8428665d79e19 to your computer and use it in GitHub Desktop.
A simple WordPress plugin that publishes scheduled posts that were missed by cron (at defined interval).

Author License DigitalOcean Twitter

Scheduled Post Trigger Plugin for WordPress

This plugin is a fork of the Scheduled Post Trigger plugin, modified for slight performance improvement and suppose for a check interval. Please feel free to suggest further performance improvements.

🚨 If your site does not support object caching, you will not likely observe any sort of performance benefit over the original plugin, since WordPress will fall back to caching in the database (incurring a penalty). If you're not sure, you can check your wp-content directory for the presence of a file named object-cache.php.

By default, this plugin will check for missed scheduled posts every 60 seconds. You can change this by modifying the plugin, or defining the TSP_CHECK_INTERVAL constant to a different interval in your site's wp-config.php. Example:

define( 'TSP_CHECK_INTERVAL', 300 ); // Every 5 minutes

Yes, I'm aware that I could have simply appended get_current_blog_id() to the cache key, but I figured that checking for a constant first was leaner.

Installation

  1. Create a file named trigger-scheduled-posts.php in your wp-content/plugins directory.
  2. Paste the code below and save.
  3. Activate the Scheduled Post Trigger plugin in WP Admin.

This plugin does not make any changes to your WordPress installation, so if you no longer want to use it, simply delete the plugin.

<?php
/**
* Plugin Name: Scheduled Post Trigger
* Description: Publishes scheduled posts that were missed by cron at defined interval
* Plugin URI: https://gist.github.com/dmhendricks/b752f93470a3e7f186f8428665d79e19/
* Version: 1.0.0
* Requires at least: 4.9
* Requires PHP: 5.6
* License: GPLv2 or later
* Author: Daniel M. Hendricks
* Author URI: https://daniel.hn/
* Forked from: https://wordpress.org/plugins/scheduled-post-trigger/
*/
/**
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace CloudVerve;
defined( 'ABSPATH' ) || die;
final class Scheduled_Post_Trigger {
private static $instance;
private static $interval = 60; // seconds
public static function init() {
if ( !isset( self::$instance ) && !( self::$instance instanceof Scheduled_Post_Trigger ) ) {
self::$instance = new Scheduled_Post_Trigger;
// Set check interval (seconds)
if( defined( 'TSP_CHECK_INTERVAL' ) && TSP_CHECK_INTERVAL ) self::$interval = TSP_CHECK_INTERVAL;
add_action( 'wp', array( self::$instance, 'publish_missed_posts' ) );
}
return self::$instance;
}
/**
* Publish missed scheduled posts
*
* @since 1.0.0
*/
public static function publish_missed_posts() {
// For better performance: Limit to front page and singles for better performance
if( !( is_front_page() || is_single() ) ) return;
// Check cache key to see if it's time to check again
$cache_key = 'trigger-scheduled-posts-interval-' . ( defined( 'MULTISITE' ) && MULTISITE ? get_current_blog_id() : 'check' ) ;
wp_cache_get( $cache_key, null, false, $already_checked );
if( $already_checked ) return;
// Get schedule posts that were missed
global $wpdb;
$missed_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'future' AND post_date_gmt < UTC_TIMESTAMP()" );
// If there are none, simply return
if( !$missed_posts ) return;
// Loop through each post ID and publish
foreach( $missed_posts as $post_id ) {
wp_publish_post( $post_id );
}
// Add cache key to signify that we've already since interval
wp_cache_set( $cache_key, true, null, intval( self::$interval ) );
}
}
Scheduled_Post_Trigger::init();
@steamboatid
Copy link

this sql works for me

SELECT ID FROM $wpdb->posts WHERE post_status = 'future' AND (post_date_gmt < UTC_TIMESTAMP() || post_date < NOW())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment