0

I've been working on a Wordpress plugin, and it requires a cron job to do some work. I've got the job setup and scheduled and I can see it in the cron using one of the plugins which lets you see the cron file.

However the function doesn't appear to actually ever get called.

The function that setup the cron is below. My code is within a class, so I'm not sure if I'm doing this correctly as none of the examples that I've found talk about if your code is within a class.

function schedule_archiving() {
    if ( ! wp_next_scheduled( 'redirection_reporting_archive_data') ) {
        $time=time();
        $time=$time+60;
        wp_schedule_event($time, 'daily', 'redirection_reporting_archive_data');
    }
}

I know that the WordPress function "archive_data" works correctly because if I call it manually from WordPress the data in the database is archived correctly, so the problem has to be my cron config (I think).

EDIT1: The code which calls schedule_archiving as requested.

if ($input['archive_enabled'] == 'true') {
            $archive = new redirector_reporting_class_archive();
            $archive->create_db_objects();
            $archive->schedule_archiving();
        } else {
            $archive = new redirector_reporting_class_archive();
            $archive->unscheduled_archiving();
            $archive->put_archive_back();
            $input['archive_enabled'] = 'false';
        }

This code is within the settings validate function of my settings class.

Below is my archive_data function which the cron job would be calling.

function archive_data() {
        write_log('redirection-reporting.archive.archive_data starting');

        global $wpdb;
        $options = get_option('redirection_reporting');
        $days_to_keep = $options['days_to_keep'];
        if ($days_to_keep != 0) {
            $days_to_keep = $days_to_keep*-1;
        }

        $sql = "CALL `{$wpdb->prefix}ArchiveRedirectionData` ($days_to_keep)";

        $wpdb->query( $sql );
    }

EDIT2: I've added the following to the where I've got the rest of the add_action and add_filter calls.

$archive = new redirector_reporting_class_archive();
add_action('redirection_reporting_archive_data', array($this, 'archive_data')); // Adds hook for cron job

You can look at the full code in the published plugin at https://wordpress.org/plugins/redirection-reporting/.

5
  • schedule_arriving is called from my settings page. I'll post the code snippit that calls schedule_arriving above.
    – mrdenny
    Jan 29, 2014 at 1:46
  • I've added that code as well as the function which the cron should be calling. Functions schedule_arriving() and archive_data() are both within the same class.
    – mrdenny
    Jan 29, 2014 at 1:50
  • I've made some progress here. The action isn't getting registered for some reason. When I used has_action it was reporting that the action wasn't ever getting setup. I've tried putting the add_action call in the plugin's activation function and at the bottom of the plugin's main file and it doesn't work from either place.
    – mrdenny
    Jan 30, 2014 at 4:27
  • @mrdenny As an update to your question, not as comment.
    – kaiser
    Feb 5, 2014 at 15:36
  • Your last link in the question is dead … could you please file an edit and fix this? Thanks.
    – kaiser
    Oct 5, 2015 at 0:48

1 Answer 1

2

You have got it wrong. The function wp_schedule_event() schedules a action hook with the third argument. So you will need to call your function on the scheduled hook. Like this -

add_action( 'archive_data', 'archive_data' );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.