Skip to main content
added 701 characters in body
Source Link
scytale
  • 424
  • 4
  • 10

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 visitsrequest spawns a background request to fire a separate wp-cron "process" but I assume (I couldn't find any info) this process won't be identified as "is-admin"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.

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 (the plugin is for both PHP 5 & 7).

I am aware front end visits fire a separate wp-cron "process" but I assume (I couldn't find any info) this process won't be identified as "is-admin" to run 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?

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.

Source Link
scytale
  • 424
  • 4
  • 10

wp-cron and execution of code in is_admin() included script

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 (the plugin is for both PHP 5 & 7).

I am aware front end visits fire a separate wp-cron "process" but I assume (I couldn't find any info) this process won't be identified as "is-admin" to run 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?