自定义Wordpress插件

时间:2016-09-19 14:03:58

标签: php wordpress cron

我有一个网站,我需要每天从外部网址导入数据,所以我做了一个插件来处理这个。到目前为止一切顺利,但问题是我的cron事件不起作用。我安装了Crontrol插件来测试事件,但没有任何反应。我在列表中看到了我的钩子名称,但是当我点击“立即运行”时,我收到一条消息,表明cron事件已成功执行,但数据未导入。

我搜索了很多recourses online(例如),但不知何故,其他地方发布的所有解决方案似乎都不适合我。我必须在某个地方错过一步。

该插件名为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插件开发世界中的第一步,所以我可以想象我错过了一个重要的钩子或过滤器(或其他),但我无法找到它是什么。也许是一些初始化?

1 个答案:

答案 0 :(得分:0)

首先,您应该为全局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/