我有一个网站,我需要从外部url每天导入数据,所以我做了一个插件来处理这个。到目前为止一切顺利,但问题是我的cron事件不起作用。我安装了Crontrol插件来测试这个事件,但是什么也没发生。我在列表中看到我的钩子名称,但是当我点击"现在运行"时,我得到一个消息,cron事件成功执行,但数据没有导入。
我已经在网上搜索了很多资源(例如),但不知何故,其他地方发布的所有解决方案似乎都不适合我。我一定是哪里漏了一步。
插件名为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插件开发世界的第一步,所以我可以想象我错过了一个重要的钩子或过滤器(或任何东西),但我就是找不到它是什么。也许是一些初始化?
首先,你应该为你的全局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';
// Register activation / deactivation hooks
register_deactivation_hook( __FILE__, 'ip_deactivation_func' );
register_activation_hook( __FILE__, 'ip_activation_func' );
// The plugin activation function
function ip_activation_func(){
// Do not forget to namespace your hooks also
if( !wp_next_scheduled( 'ip_import_data' ) ){
wp_schedule_event( time(), 'daily', 'ip_import_data' );
}
}
// The plugin deactivation function
function ip_deactivation_func(){
wp_clear_scheduled_hook( 'ip_import_data' );
}
// Add the action event hook
add_action( 'ip_import_data', 'ip_do_import_data' );
// Your actual event code:
function ip_do_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();
// Added this part, no need to use: $wp_query object here!
global $post;
$post_id = $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();
}
}
我不知道你的事件代码,你可能需要运行它以确保它正常工作。
在这里了解更多关于WP cron的信息:https://developer.wordpress.org/plugins/cron/
在这里了解更多关于激活/停用钩子的信息:https://developer.wordpress.org/plugins/the-basics/activation-deactivation-hooks/
和一个很好的插件来调试wp cron事件:https://wordpress.org/plugins/wp-crontrol/