Creating a Scheduled Event
首先,我们将创建一个用于安排活动的函数。我的名为“mycronjob”,每天运行一次。所有这些代码都可以进入插件的主文件,在主功能之外:
// create a scheduled event (if it does not exist already)
function cronstarter_activation() {
if( !wp_next_scheduled( \'mycronjob\' ) ) {
wp_schedule_event( time(), \'daily\', \'mycronjob\' );
}
}
// and make sure it\'s called whenever WordPress loads
add_action(\'wp\', \'cronstarter_activation\');
Adding your Repeat Function
这只是您希望定期运行的代码的占位符:
// here\'s the function we\'d like to call with our cron job
function my_repeat_function() {
// do here what needs to be done automatically as per your schedule
// in this example we\'re sending an email
// components for our email
$recepients = \'[email protected]\';
$subject = \'Hello from your Cron Job\';
$message = \'This is a test mail sent by WordPress automatically as per your schedule.\';
// let\'s send it
mail($recepients, $subject, $message);
}
// hook that function onto our scheduled event:
add_action (\'mycronjob\', \'my_repeat_function\');
After all that if you face some problem with specific time then you can check this answer Here