Simple Testing of WP-Cron

In this tutorial we will be creating a plugin to show all the currently scheduled WPCron tasks. In order to test this we will load the wp-cron.php file directly in our browser and output data to the display (otherwise we would have to perform some other action, maybe in the database, as the output is typically not shown on the site).

So let’s run through the initial steps to get this setup quickly.

Create the plugin file Create the plugin file

In the wp-content/plugins folder create a new folder called my-wp-cron-test and, inside this folder, a new file called my-wp-cron-test.php.

Open the PHP file for editing and insert the following lines:

<?php
/*
Plugin Name: My WP-Cron Test
*/

This text will setup the plugin for display and activation in the Plugins menu. Find the new plugin there, and activate it.

Top ↑

View all currently scheduled tasks View all currently scheduled tasks

WordPress has an undocumented function, _get_cron_array(), that returns an array of all currently scheduled tasks. We are going to var_dump to show all the scheduled tasks.

Put the following code in the PHP file:

echo '<pre>'; print_r( _get_cron_array() ); echo '</pre>';

You can also put this code into an easy-to-call function like this:

function bl_print_tasks() {
    echo '<pre>'; print_r( _get_cron_array() ); echo '</pre>';
}
bl_print_tasks();
Note: Remember, the “bl_” part of the function name is a function prefix. You can learn why prefixes are important here.

The finished file looks like this:

<?php
/*
Plugin Name: My WP-Cron Test
*/

function bl_print_tasks() {
    echo '<pre>'; print_r( _get_cron_array() ); echo '</pre>';
}
bl_print_tasks();

Top ↑

Testing the code Testing the code

Open your browser and point it to YOUR_SITE_URL/wp-cron.php. You’ll see listed all the scheduled tasks for you installation.

This works because every time WordPress loads a page, it initializes all installed plugins by loading the main file for each plugin and running what it finds inside. In our case, the plugin is doing only one thing: showing the list of scheduled tasks.

Note: If you have installed WordPress into a subfolder, you’ll need to add that subfolder to the URL. This is something you would have to do intentionally, so if you don’t know what this means, you can probably ignore this :)