Skip to content

Scheduled Tasks / WP Cron #15

@flowdee

Description

@flowdee

Would love to add support for OhDear's scheduled tasks and track WP cron execution, so you can monitor if it runs properly on your site.

Bildschirm­foto 2023-03-13 um 18 36 35

When you set up a "Scheduled Task" in OhDear, they give you a "Ping URL" which needs to be called frequently.

Besides that, you can set up the frequency how often the URL should be called (Grace period is unimportant for our implementation).

So the idea is, that on our settings page, we add a new section "Scheduled Tasks (WP Cron)", as well as the following settings:

  • Ping URL (url field)
  • Frequency (number field; values from 1 to 60; no comma)

When Ping url or frequency are empty, our plugin does not do anything here at all.

Once input is available, our plugin sets a custom cron interval based on the settings given: https://developer.wordpress.org/plugins/cron/understanding-wp-cron-scheduling/

For the event function, we just call the entered ohdear ping url.

Here's a example I currently used for our sites:

/*
 * Setup cron interval
 */
add_filter( 'cron_schedules', function ( $schedules ) {
   $schedules['fdmedia_cron_check'] = array(
       'interval' => 300,
       'display' => __( 'Every 5 Minutes' )
   );
   return $schedules;
} );

/*
 * Schedule cron event
 */
if ( ! wp_next_scheduled( 'fdmedia_site_cron_check' ) ) {
    wp_schedule_event( time(), 'fdmedia_cron_check', 'fdmedia_site_cron_check' );
}

/*
 * Cron check function
 */
add_action('fdmedia_site_cron_check', function() {
	// Call OhDear API
	$response = wp_remote_get( 'https://ping.ohdear.app/d0a8ea91-dd58-400b-8a3a-1835a6126f4e' );
	// Do nothing.
});

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions