Skip to content

Instantly share code, notes, and snippets.

@SiR-DanieL SiR-DanieL/functions.php
Last active Feb 8, 2018

Embed
What would you like to do?
<?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
You can’t perform that action at this time.