Search code examples
wordpresscroncrontrigger

Set a wordpress cron job to hit a url every 2 minutes


Hi I have a url which imports data from csv to database on wordpress.I want this to run every 2 minutes .Is tried a plugin for this cron job scheduer but this did not work. How can I write a action script function make this possibe.Please help


Solution

  • You can do the following
    First you need to add the 2m interval to the schedule

    add_filter('cron_schedules', 'my_schedules');
    
    function my_schedules($schedules)
    {
        $schedules['once_every_2m'] = array('interval' => 120, 'display' => 'Once every 2 minutes');
        return $schedules;
    }
    

    Then you add your job using newly created interval

    if (!wp_next_scheduled('name_of_your_job'))
    {
        wp_schedule_event(1481799444, 'once_every_2m', 'name_of_your_job');
    }
    add_action('name_of_your_job', 'function_that_should_be_executed');
    
    function function_that_should_be_executed()
    {
        //do what you need to do
    }
    

    Also, keep in mind, due to how WP cron works, it might be in accurate with timing. Docs