Using WordPress ‘pre_get_ready_cron_jobs’ PHP filter

pre_get_ready_cron_jobs is a WordPress PHP filter that allows you to modify or intercept the retrieval of ready cron jobs before they are executed.

Usage

add_filter( 'pre_get_ready_cron_jobs', 'your_custom_function_name', 10, 1 );

function your_custom_function_name( $pre ) {
  // your custom code here
  return $pre;
}

Parameters

  • $pre (null|array[]): An array of ready cron tasks to return instead. Default is null, which continues using results from _get_cron_array().

More information

See WordPress Developer Resources: pre_get_ready_cron_jobs

Examples

Log all ready cron jobs

Log all ready cron jobs before they are executed.

add_filter( 'pre_get_ready_cron_jobs', 'log_ready_cron_jobs', 10, 1 );

function log_ready_cron_jobs( $pre ) {
  // Log the ready cron jobs
  error_log( print_r( $pre, true ) );
  return $pre;
}

Skip a specific cron job

Skip a specific cron job by removing it from the ready cron jobs array.

add_filter( 'pre_get_ready_cron_jobs', 'skip_specific_cron_job', 10, 1 );

function skip_specific_cron_job( $pre ) {
  // Remove the specific cron job
  unset( $pre['your_cron_hook'] );
  return $pre;
}

Add a custom cron job

Add a custom cron job to the ready cron jobs array.

add_filter( 'pre_get_ready_cron_jobs', 'add_custom_cron_job', 10, 1 );

function add_custom_cron_job( $pre ) {
  // Add the custom cron job
  $pre['custom_cron_hook'] = time();
  return $pre;
}

Replace all cron jobs with a custom one

Replace all ready cron jobs with a single custom cron job.

add_filter( 'pre_get_ready_cron_jobs', 'replace_with_custom_cron_job', 10, 1 );

function replace_with_custom_cron_job( $pre ) {
  // Replace all cron jobs with a custom one
  $pre = array( 'custom_cron_hook' => time() );
  return $pre;
}

Execute specific cron job immediately

Execute a specific cron job immediately by setting its timestamp to the current time.

add_filter( 'pre_get_ready_cron_jobs', 'execute_specific_cron_job_immediately', 10, 1 );

function execute_specific_cron_job_immediately( $pre ) {
  // Set the timestamp of the specific cron job to the current time
  $pre['your_cron_hook'] = time();
  return $pre;
}