如何在Wordpress中插入带有cron作业的帖子 [英] How to insert post with cron job in Wordpress
本文介绍了如何在Wordpress中插入带有cron作业的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个插件,每30分钟使用cron作业创建一个新的帖子。
我已经为我的cron作业创建了一个新的时间间隔。
我认为问题是插入一个新的职位。我不知道你能帮我吗?
I want to make a plugin that that creates a new posts every 30 minutes using cron job. I have already create a new interval for my cron job. I think tha problem is in inserting a new post. I am not sure can you help me?
<?php
/*
Plugin Name:
Description:
Version: 1.0
Author:
*/
add_filter('cron_schedules', 'new_interval');
// add once 30 minute interval to wp schedules
function new_interval($interval) {
$interval['minutes_30'] = array('interval' => 30*60, 'display' => 'Once 30 minutes');
return $interval;
}
function InitiateMyCron() {
if (!wp_next_scheduled('MyCronEvent')) {
wp_schedule_event(time(), 'minutes_30', 'MyCronAction');
}
}
function MyCronAction() {
//do my cron every 30 minutes
$new_post = array(
'post_title' => 'Cron job New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => 1,
'post_type' => 'post',
'post_category' => array(6, 2)
);
$post_id = wp_insert_post($new_post);
}
?>
我做错了什么?
先谢谢。
推荐答案
不要忘记WordPress cron机制仍然是非自动模拟器。如果访客(人民,搜索引擎等)每分钟访问您的网站 - cron将工作。如果访问者没有触摸您的网站 - cron将无法工作。
Don't forget WordPress cron "mechanism" is still "non-automatic" emulator. If visitors (peoples, search engines, etc) are visiting your website every minutes - cron will work. If visitors don't "touch" your website - cron will not work.
这篇关于如何在Wordpress中插入带有cron作业的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文