【发布时间】:2017-01-27 06:13:57
【问题描述】:
我有一个网站,我需要每天从外部 url 导入数据,所以我制作了一个插件来处理这个问题。到目前为止一切顺利,但问题是我的 cron 事件不起作用。我安装了 Crontrol 插件来测试事件,但没有任何反应。我在列表中看到了我的钩子名称,但是当我单击“立即运行”时,我收到一条消息,指出 cron 事件已成功执行,但未导入数据。
我搜索了很多recoursesonline(例如),但不知何故,其他地方发布的所有解决方案似乎都不适合我。我一定是在某个地方漏掉了一步。
这个插件叫做 import-data 并且在 wp-content/plugins/import-data/ 我有 import-data.php:
<?php
/**
* Plugin Name: Import data
* Plugin URI:
* Description: Import data
* Version: 1.0.0
* Author:
* Author URI:
* License: GPL2
*/
// Block direct acces to file
defined('ABSPATH') or die();
// Include functions
require_once dirname( __FILE__ ).DIRECTORY_SEPARATOR.'functions.php';
// Include class
require_once dirname( __FILE__ ).DIRECTORY_SEPARATOR.'lib/class.import_data.php';
/**
* @desc iterate through all posts and update information
*/
function import_data(){
$wp_query = new WP_Query(
array(
'post_type' => 'post',
'post_status' => 'publish',
)
);
if($wp_query->have_posts()){
while($wp_query->have_posts()){
$wp_query->the_post();
$post_id = $wp_query->post->ID;
$external_id = get_field(trim(get_option('acfname_external_id')));
// Execute plugin
Import_Data::getInstance()->fetchDetails($external_id, $post_id);
}
wp_reset_postdata();
}
}
/**
* Set cron
*/
function my_event(){
if(!wp_next_scheduled('import_data')){
wp_schedule_event(time(), 'daily', 'import_data');
}
}
add_action('wp', 'my_event');
function unset_event(){
wp_clear_scheduled_hook('import_data');
}
register_deactivation_hook(__FILE__, 'unset_event');
我知道fetchDetails() 方法有效,因为我在手动运行它之前和当我手动运行它时测试了输出(我已经向import_data() 添加了一个短代码并在演示页面上使用它)数据被导入,但是上面的 cron 设置没有。
functions.php 中只有管理页面设置。
这是我在 Wordpress 插件开发领域的第一步,因此我可以想象我错过了一个重要的钩子或过滤器(或其他任何东西),但我就是找不到它是什么。也许一些初始化?
【问题讨论】: