Created
March 17, 2014 16:22
-
-
Save codescribblr/9602578 to your computer and use it in GitHub Desktop.
This creates a recurring cron job in Wordpress.
From http://wp.smashingmagazine.com/2013/10/16/schedule-events-using-wordpress-cron/
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
| //On plugin activation schedule our daily database backup | |
| register_activation_hook( __FILE__, 'wi_create_daily_backup_schedule' ); | |
| function wi_create_daily_backup_schedule(){ | |
| //Use wp_next_scheduled to check if the event is already scheduled | |
| $timestamp = wp_next_scheduled( 'wi_create_daily_backup' ); | |
| //If $timestamp == false schedule daily backups since it hasn't been done previously | |
| if( $timestamp == false ){ | |
| //Schedule the event for right now, then to repeat daily using the hook 'wi_create_daily_backup' | |
| wp_schedule_event( time(), 'daily', 'wi_create_daily_backup' ); | |
| } | |
| } | |
| //Hook our function , wi_create_backup(), into the action wi_create_daily_backup | |
| add_action( 'wi_create_daily_backup', 'wi_create_backup' ); | |
| function wi_create_backup(){ | |
| //Run code to create backup. | |
| } | |
| register_deactivation_hook( __FILE__, 'wi_remove_daily_backup_schedule' ); | |
| function wi_remove_daily_backup_schedule(){ | |
| wp_clear_scheduled_hook( 'wi_create_daily_backup' ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment