WP Schedule Event – Every Day When First Visitor Comes

I have a daily wp schedule event.

I know that it happens when a user visits the site and if a schedule time passes.

If I started it on today 12 a.m. and if tomorrow first visitor comes tomorrow 1:20 a.m. that event happen tomorrow 1:20 a.m..

Day after day, it happens after 1.20 a.m. or after 12 a.m.?

Solutions Collecting From Web of "WP Schedule Event – Every Day When First Visitor Comes"

wp-cron is often called a pseudo-cron, because it doesn’t run on a strict schedule. If nobody visits the site, it doesn’t run. If you schedule a wp-cron event to run, say, every 12 hours, it will run at most every 12 hours. But, if your site has very little traffic, there could be far more than 12 hours between runs.

If you need an event to happen every 12 hours, or every day at the same time, you’ll need to use something outside of WordPress. Common approaches to that include using the system task scheduler (on Linux, this is usually called “cron”) or a third-party service like EasyCron.

Assuming you’re on a Linux server (by far the most common hosting environment), create a small shell script that looks like this:

#!/bin/bash
curl http://yoursite.name.com/wp-cron.php >/dev/null

This will run the “curl” command, to load your site’s wp-cron.php page, then immediately ignore the results (by dumping the output to /dev/null). You don’t care about any output the page might have, and in fact there normally isn’t any output. You just want to be sure the page is loaded.

Then, add a line to your crontab (“crontab -e” will open the crontab file in your default editor, probably vi) that looks something like this:

0 0,12 * * *             /home/user/touch-site.sh

(Obviously, modify the file location to wherever you put your script. Also, be sure the script is executable by running “chmod 700 touch-site.sh”.)

This example will visit your site daily, at noon and midnight (server time), and run the wp-cron script. For more details on how this file works, run “man crontab”.

To answer your this query

@sumit Are you sure about that? One of my friend said that if today it happens in 1.20 a.m next day it will happen after 1.20 a.m, not the 12 a.m.

How it works:

  1. You schedule an event using current time stamp i.e. 12:00 AM on Monday

    wp_schedule_event(time(), 'daily', 'my_schedule_hook');
    
  2. Someone visited the site on Monday at 1:20 AM.

WP call this piece of code

    if ( $schedule != false ) {
            $new_args = array($timestamp, $schedule, $hook, $v['args']);
            call_user_func_array('wp_reschedule_event', $new_args);
    }

    wp_unschedule_event( $timestamp, $hook, $v['args'] );

   /**
    * Fires scheduled events.
    *
    * @ignore
    * @since 2.1.0
    *
    * @param string $hook Name of the hook that was scheduled to be fired.
    * @param array  $args The arguments to be passed to the hook.
    */
    do_action_ref_array( $hook, $v['args'] );

Here it is calling wp_reschedule_event() function which uses the same timestamp so the next schedule will be Tuesday 12:00 AM NOT 1:20 AM. Then it will call schedule action hook.

  1. Common mistake people do is they call wp_schedule_event() directly in functions.php so WP schedule event on every new timestamp. The correct way is check if schedule is there only then create it.

    $my_schedule_hook = wp_next_scheduled('my_schedule_hook');
    if (!$my_schedule_hook) {
        wp_schedule_event(time(), 'daily', 'my_schedule_hook');
    }
    

Thus it will only go inside the if block only at first time when there will be no schedule at all.
Still you can view all the schedule by this plugin WP Crontrol