Last active
February 8, 2018 16:10
-
-
Save SirDarcanos/f4f3889f1789dc6998d4899041c89b0b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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