Created
April 3, 2018 07:51
-
-
Save dcangulo/3f8b42e21ba872aeb736e68e74774f34 to your computer and use it in GitHub Desktop.
A simple WordPress plugin that has a cron schedule. Visit https://www.davidangulo.xyz/ for more information.
This file contains 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: My cron WordPress plugin | |
Plugin URI: wordpress.org/plugins | |
Description: A simple WordPress plugin that executes many times in a certain interval. | |
Version: 1.0.0 | |
Author: David Angulo | |
Author URI: https://www.davidangulo.xyz/ | |
License: GPL2 | |
*/ | |
register_activation_hook( __FILE__,"register_schedule"); | |
function register_schedule() { | |
if(!wp_next_scheduled("my_daily_event")) { | |
wp_schedule_event(strtotime("06:00:00"),"daily","my_daily_event"); | |
} | |
} | |
add_action("my_daily_event","do_this_daily"); | |
function do_this_daily() { | |
wp_mail("some.email@example.xyz","Morning Message","Good Morning! Have a nice day. :)"); | |
} | |
register_deactivation_hook( __FILE__,"remove_schedule"); | |
function remove_schedule() { | |
wp_clear_scheduled_hook("my_daily_event"); | |
} | |
add_filter("cron_schedules","custom_cron_schedules"); | |
function custom_cron_schedules($schedules){ | |
if(!isset($schedules["5minutes"])){ | |
$schedules["5minutes"] = array( | |
'interval' => 5*60, | |
'display' => __('Once every 5 minutes')); | |
} | |
if(!isset($schedules["halfhour"])){ | |
$schedules["halfhour"] = array( | |
'interval' => 30*60, | |
'display' => __('Once every 30 minutes')); | |
} | |
return $schedules; | |
} | |
//Visit the tutorial for more information | |
//https://www.davidangulo.xyz/website-development/how-to-create-cron-job-in-wordpress/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment