/**
*WordPress 自動定時清除垃圾評論
*https://www.weixiaoduo.com/cron-delete-all-spam/
*/
classBing_cron_delete_spam{
//每隔多少秒清理垃圾評論
public$time=604800;//自己修改時間,604800 為一週
//Hook 名
public$hook='delete_all_spam';
//初始化
function__construct(){
add_action('init',array($this,'cron'));
add_filter('cron_schedules',array($this,'schedules'));
add_action($this->hook,array($this,'delete'));
}
//設置定時任務
functioncron(){
if(!wp_next_scheduled($this->hook))wp_schedule_event(current_time('timestamp'),$this->hook,$this->hook);
}
//擴展定時器
functionschedules($schedules){
$schedules[$this->hook]=array(
'interval'=>$this->time,
'display'=>__('清除垃圾評論')
);
return$schedules;
}
//刪除垃圾評論
functiondelete(){
global$wpdb;
$wpdb->delete($wpdb->comments,array('comment_approved'=>'spam'));
}
}
$cron_delete_spam=newBing_cron_delete_spam;