Skip to content

Instantly share code, notes, and snippets.

@SiR-DanieL
Last active February 8, 2018 16:10
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save SiR-DanieL/f4f3889f1789dc6998d4899041c89b0b to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Example Scheduled Events
Plugin URI: https://wordpress.org/
Description: This is an example plugin to learn how to use scheduled events
Version: 1.0.0
Author: Your Name
Author URI: https://yoursite.com
Text Domain: egse
*/
/**
* Schedule the event when the plugin is activated, if not already scheduled
* and run it immediately for the first time
*/
register_activation_hook( __FILE__, 'my_activation' );
function my_activation() {
if ( ! wp_next_scheduled ( 'my_hourly_event' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'my_hourly_event' );
}
}
/**
* Hook the function to run every hour
*/
add_action('my_hourly_event', 'do_this_hourly');
function do_this_hourly() {
error_log( 'I\'m alive!' );
}
/**
* Clear the scheduled event when the plugin is disabled
*/
register_deactivation_hook( __FILE__, 'my_deactivation' );
function my_deactivation() {
wp_clear_scheduled_hook( 'my_hourly_event' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment