write a code snippet to check that cron is working on a website

WPTurbo » Snippets » write a code snippet to check that cron is working on a website
0

Created with:

Visibility: 

public

Creator: Stephen Oxlade

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php

<?php

namespace WPTurbo;

defined('ABSPATH') || exit; // Exit if accessed directly

/**
 * Check if the cron event is scheduled, and schedule it if not.
 */
function wpturbo_check_cron() {
    // Check if the 'wpturbo_test_cron_event' is already scheduled
    if (! wp_next_scheduled('wpturbo_test_cron_event')) {
        // Schedule the event to run hourly
        wp_schedule_event(time(), 'hourly', 'wpturbo_test_cron_event');
    }
}

/**
 * Callback function for the cron event.
 * This function will be called when the scheduled cron event runs.
 */
add_action('wpturbo_test_cron_event', 'wpturbo_cron_test_callback');

function wpturbo_cron_test_callback() {
    // Log the current time to confirm that the cron event is working
    $current_time = current_time('mysql');
    error_log("Cron is working correctly at " . esc_html($current_time));
}

// Initiate the cron check function to ensure the event is scheduled
wpturbo_check_cron();
				

These comments enhance the readability of the code, making it easier for other developers (or yourself in the future) to understand the functionality and purpose of each part of the code.

Register an account to save your snippets or go Pro to get more features.