wordpress用插件添加自定义的cron任务

在插件入口文件里添加如下语句:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//On plugin activation schedule our daily api soap
register_activation_hook( __FILE__, 'iggo_create_daily_soap_schedule' );
function iggo_create_daily_soap_schedule(){
//Use wp_next_scheduled to check if the event is already scheduled
$timestamp = wp_next_scheduled( 'iggo_create_daily_soap' );

//If $timestamp == false schedule daily soaps since it hasn't been done previously
if( $timestamp == false ){
//Schedule the event for right now, then to repeat daily using the hook 'iggo_create_daily_soap'
wp_schedule_event( time(), 'daily', 'iggo_create_daily_soap' );
}
}
//Hook our function , iggo_create_soap(), into the action iggo_create_daily_soap
add_action( 'iggo_create_daily_soap', 'iggo_create_soap' );
function iggo_create_soap(){
//Run code to create soap.
//具体任务,比如我的是每日执行一次根目录下的soap.php文件
require_once ABSPATH . 'iggo-soap.php';
//error_log("zouhongzhao", 3, "/home/wordpress/temp/zou.log");

}

 

坚持原创技术分享,您的支持将鼓励我继续创作!