0

I am developing a plugin. Among other things its backend settings will allow admins to update a site data file from a 3rd party data provider, or to switch on/off a wp-cron job to do this automatically.

In this (simplified) code I assume my wp-cron job will only be run if someone logs in to the WP Dashboard? (not what I really want)

if (is_admin()) {
  include 'my_update_script.php';
  include 'settings_form_stuff.php'; // schedules/deschedules xyz_job etc
}

and in "my_update_script.php":

function xyz_2weekly( $schedules ) {
  $schedules['xyz_2weekly'] = array( 'interval' => 3*604800, 'display' => __('Two Weeks') );
  return $schedules;
}
add_filter( 'cron_schedules', 'xyz_2weekly');

add_action( $xyz_job, 'xyz_update_files' );
function (xyz_update_files) {
  $xyz_update = new xyz_upd_class();
  $xyz_update->do_update(TRUE); // true identifies it as automated and emails admin on failure
  unset($xyz_update);
  }

class xyz_upd_class { // etc

Inefficiencies add up, and I want to avoid overhead of unnecessary includes on the front end (the plugin is for both PHP 5 & 7).

I am aware that to prevent delay in response to the visitor the front end request spawns a background request to fire a separate wp-cron "process" but I assume (I couldn't find any info) this process isn't set to run in "wp-admin/is-admin scope" and so won't execute the add_action.

Is there an efficient alteration I can make so the update script (with add_action) is not included on front end requests but is still available to wp-cron when fired from front-end?

Edit: Clinton's answer indicates that my is_admin include would be "out of scope" when "wp-cron process" is spawned by front-end request.

I was hoping for answers similar to this if ( is_admin() || wp_next_scheduled($xyz_job) ) include ... (one I thought of but ruled out, as most sites will have scheduled the job; and I suspect schedule won't be in memory so this would just result in an additional "slow" server read).

In absence of alternatives, I have decided to just incorporate the code without checking if admin so I am accepting Clinton's answer.

1

I had to read this a few times to understand it and I am not sure if my interpretation is correct. Anyhow here is my interpretation (apologies if I have gotten this wrong)...

You have a CRON job that is intended to update files of some sort. This CRON job will run when someone visits the site whether admin or not at your specified frequency. The CRON job can be switched off by an administrator from the plugin settings. You do not want to include any unnecessary includes.

I would split this into 2 separate parts first the settings part where I would set a on/off flag as follows:

define the flag as an option setting and set it to on (1) when the plugin is activated:

register_activation_hook(__FILE__, function(){
    add_option('xyz_cron_status', 1);
}

The value of this option will be changed through a form accessed through the plugin settings menu in the usual Wordpress Plugin way.

Next define your CRON job in the plugin setup code (not surrounded by is_admin() function) as follows:

xyz_cron_option = get_option('xyz_cron_status');
if ( xyz_cron_option ){
    add_filter('cron_schedules', 'xyz_2weekly');
    add_action('wp', 'xyz_cron_settings');
    add_action('xyz_cron_hook', 'xyz_update_files');
}

where xyz_cron_settings is as follows:

/**
 * Create a hook called xyz_cron_hook for this plugin and set the time
 * interval for the hook to fire through CRON
 */
function xyz_cron_settings() {
    //verify event has not already been scheduled
    if ( !wp_next_scheduled( 'xyz_cron_hook' ) ) {
        wp_schedule_event( time(), 'xyz_2weekly', 'xyz_cron_hook' );
    }
}

the other functions are as shown in your code:

function xyz_2weekly( $schedules ) {
  ...
}

and

function xyz_update_files() {
  ...
}

I hope that this helps.

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.