Check if function called by cron job

I have a function that I only want to run via a cron job. Is there a way I can check that a particular scheduled event is calling this function and not anything else?

Related posts

Leave a Reply

3 comments

  1. WordPress has a constant DOING_CRON that helps us know we’re doing the cron jobs or not. It’s defined in wp-cron.php file.

    So, you can check this constant in your code:

    if ( defined( 'DOING_CRON' ) )
    {
        // Do something
    }
    
  2. Just take a look at the »Cron API inspector«, that I wrote for question #18017. The plugin builds a table that shows on the shutdown-hook/the end of the page.

    It uses the _get_cron_array() function to retrieve all registered & scheduled actions. Another useful function is wp_get_schedules(). The third way is calling the underlying data for _get_cron_array() directly, by calling get_option( 'cron' ); – it’s still better to use the default API functions from WP core.

    If you want to know if you’re currently inside a Cron Job, then you can check defined( 'DOING_CRON' ) AND DOING_CRON.

  3. I’m not studies development (I’m just an enthusiast) but I think you may add a add_action to the event

    It’s just a resume…

    //to the event
    if(your_condition)
    {
        add_action('original_event_to_hook', 'your_scheduled');
    }
    
    function your_scheduled()
    {
        //create a param here
        //And shedule your function with arg
        wp_schedule_event(time(), 'hourly', 'your_function', array('param_1' => value));
    }
    
    function your_function($args){
    
       $verification = $args['param_1'];
       if($verification)
       {
          //OK
          //Eventually delete this schedule with this specific arg
       }
    }
    

    To retrieve a list of your cron “your_function” where have this arg

         //Get a filtered list of cron hooks 
            function kw_filter_crons_hooks($hooks = false, $args = false)
            {
                $crons = get_option('cron');
    
                if (!$crons)
                {
                    $crons[0] = NULL;
                }
    
                $filter = array();
                $cronlist = array();
                $schedule = array();
    
                foreach($crons as $timestamp => $cron)
                {
                    //@param $hooks = string 'schedule'
                    //@param $args = false
                    //Return an array of cron task sheduled
                    $schedule[] = $cron;
    
                    if(!$schedule && $hooks == 'schedule' && $args == false)
                    {
                        $schedule[0] = NULL;
                    }
    
                    foreach($hooks as $hook)
                    {
                        if(isset($cron[$hook]))
                        {
                            //@param $hooks = array($hook);
                            //@param $args = false
                            //Return an array of cron task sheduled
                            $cronlist[] = $cron;
    
                            if(!$cronlist && is_array($hooks) && $args == false)
                            {
                                $cronlist[0] = NULL;
                            }
    
                            if(!empty($args))
                            {
                                foreach($cronlist as $key => $value)
                                {
                                    foreach($value as $k => $v)
                                    {
                                        foreach($v as $x => $y)
                                        {
                                            foreach($args as $arg => $val)
                                            {
                                                if ($y['args'][$arg] == $val)
                                                {
                                                    //@param $hooks = array($hook);
                                                    //@param $args = array($arg);
                                                    //Return an array of cron task specified filtered by arg
                                                    $filter[$x] = $y;
                                                    if(!$filter && is_array($hooks) && is_array($args))
                                                    {
                                                        $filter[0] = NULL;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
    
    
                if(is_array($hooks) && $args === false && $cronlist)
                {
                    return $cronlist;
                }
                else if(is_array($hooks) && is_array($args) && $filter)
                {
                    return $filter;
                }
                else if($hooks === 'schedule' && $args === false && $schedule)
                {
                    return $schedule;
                }
                else if($hooks === false && $args === false && $crons)
                {
                    return $crons;
                }
                else
                {
                    return false;
                }
            }
    
    //Usage
        $cron_filtered = kw_filter_crons_hooks(array('your_function'), array('param_1' => value));
        var_dump($cron_filtered);