0
votes

I have a site where i need to import data daily from an external url so i made a plugin to handle this. So far so good, but the thing is that my cron event doesn't work. I installed Crontrol plugin to test the event, but nothing happens. I see my hook name in the list, but when i click on 'Run now' i get a message that the cron event is successfully executed, but the data isn't imported.

I've searched through a lot of recourses online (for example), but somehow all the solutions posted elsewhere don't seem to work for me. I must be missing a step somewhere.

The plugin is called import-data and in wp-content/plugins/import-data/ i have 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');

I know that the method fetchDetails() works because i tested the output before and when i manually run it (i've added a shortcode to import_data() and used that on a demo page) the data gets imported, but the cron settings above don't.

In functions.php are only admin page settings.

This are my first steps in the world of plugin development for Wordpress so i can image that i miss an important hook or filter (or whatever), but i just can't find what it is. Perhaps some initialisation?

1

1 Answers

0
votes

First of you should prefix your global php functions to avoid conflicts with other plugins, themes or core.

I would use the activation hook to schedule the event, here is how I would do this:

<?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();
    }
}

I do not know about your event code, you might need to run it to make sure its working correctly.

Learn more about WP cron here: https://developer.wordpress.org/plugins/cron/

Learn more about activation / deactivation hooks here: https://developer.wordpress.org/plugins/the-basics/activation-deactivation-hooks/

And a good plugin to debug wp cron events: https://wordpress.org/plugins/wp-crontrol/