HOW TO: Add a completely useless wp-cron to WordPress

May 3, 2022

Adds a wp-cron to WordPress that does nothing. But it will do nothing... every 60 seconds (to help you troubleshoot cron reliability issues).

<?php
/**
 * Plugin Name: Useless WP Cron
 * Version: 0.0.1
 * Plugin URI: N/A
 * Description: Adds a wp-cron to WordPress that does nothing. But it will do nothing... every 60 seconds.
 * Author: 403 Page Labs
 * Author URI: https://403.ie
 * Requires at least: 5.3
 * Tested up to: 5.9.3
 *
 * Text Domain: useless-wp-cron
 * Domain Path: /lang/
 *
 * @package WordPress
 * @author 403PageLabs
 * @since 0.0.1
 */

// Define a 60 second schedule
function add_cron_schedule_60($schedules){
    if(!isset($schedules["60 Seconds"])){
        $schedules["60 Seconds"] = array(
            'interval' => 1*60,
            'display' => __('Once every minute'));
    }
    return $schedules;
}

// Add 60 seconds to wp-cron schedule list
add_filter('cron_schedules','add_cron_schedule_60');

// Ensure the useless cron that does nothing is scheduled for every 60 seconds
if ( ! wp_next_scheduled( 'a_cron_that_does_nothing' ) ) {
    wp_schedule_event( time(), '1min', 'a_cron_that_does_nothing' );
}

add_action( 'a_cron_that_does_nothing', 'useless_cron' );

// Notify the error log that the useless cron ran successfully... and did nothing
function useless_cron() {
    error_log( '[WP] A useless cron ran and did nothing. Nice! The URI was: ' . $_SERVER['REQUEST_URI'], 0 );
}