Use the wp-cron plug-in WordPress to set a scheduled task _ php instance

Source: Internet
Author: User
This article mainly introduces how to use the wp-cron plug-in WordPress to set scheduled tasks. This article provides several common PHP functions and parameters, if you need it, you can refer to PHP, which cannot create scheduled tasks. However, WordPress comes with a pseudo scheduled task (Cron) API, which is very convenient and easy to use, the regular publishing of articles including WordPress itself relies on this API.

What is WP Cron? WordPress is a set of timed triggering mechanism that allows you to schedule task execution cyclically. for example, you can regularly publish new articles and regularly check versions.

What can WP Cron implement for us? We can update and submit website data cyclically, and regularly send greeting cards or forms to readers during festivals...


The principle is to store the created scheduled task in the database. When someone accesses the task, it determines whether the scheduled task needs to be executed at the time. if the scheduled task is executed at the time, it is executed.

Due to this principle, the execution time may be somewhat different. However, as the number of website views increases and the number of Web crawlers continuously accesses, the scheduled task execution time becomes more accurate.

WP-Cron is inefficient, but it is easy to use. The following describes how to use relevant functions.

Function

Wp_get_schedule

Get the predefined hooks by using the hook alias. if the call is successful, the cycle type (hourly, twicedaily, daily,...) is returned. if the call fails, false is returned.

<?php wp_get_schedule( $hook, $args ) ?>

$ Hook: hook alias
$ Args: array of parameters of the function corresponding to the hook (optional)

Wp_get_schedules

WordPress supports hourly, twicedaily, and daily by default. with this function, we can obtain all these cyclic arrays.

<?php wp_get_schedules() ?>

By default, the array objects obtained by the preceding method are as follows.

array( 'hourly' => array( 'interval' => 3600, 'display' => 'Once Hourly' ), 'twicedaily' => array( 'interval' => 43200, 'display' => 'Twice Daily' ), 'daily' => array( 'interval' => 86400, 'display' => 'Once Daily' ))

We can add more types to the cron_schedules filter. The example is as follows:

Add_filter ('cron _ schedules ', 'cron _ add_weekly'); function cron_add_weekly ($ schedules) {// Adds once weekly to the existing schedules. $ schedules ['weekly '] = array ('interval' => 604800, // one week = 60 seconds * 60 Minutes * 24 hours * 7 days 'display' => _ ('Once Weekly '); return $ schedules;} wp_next_scheduled

Get the next scheduled running time using the hook alias and return it as an integer. it is often used to determine whether the scheduled time has been completed.

<?php $timestamp = wp_next_scheduled( $hook, $args ); ?>

$ Hook: hook alias
$ Args: array of parameters of the function corresponding to the hook (optional)

Wp_schedule_event

Schedule a WordPress hook by cycle and trigger the corresponding function at the specified time.

<?php wp_schedule_event($timestamp, $recurrence, $hook, $args); ?>

$ Timestamp: Time (integer)
$ Recurrence: cycle type (hourly, twicedaily, daily ,...)
$ Hook: hook alias
$ Args: array of parameters of the function corresponding to the hook (optional)

Wp_reschedule_event

Schedule a WordPress hook by cycle and re-schedule it. but I found that this method is not working properly and Codex is very well written. if anyone knows how to use it, please let me know.

Wp_unschedule_event

Cancels the reservation by specifying the specified time and alias.

<? Php wp_unschedule_event ($ timestamp, $ hook, $ args);?>
$ Timestamp: Time (integer)
$ Hook: hook alias
$ Args: array of parameters of the function corresponding to the hook (optional)

Wp_clear_scheduled_hook

Remove the predefined hooks by using the hook alias.

<? Php wp_clear_scheduled_hook ($ hook);?>
$ Hook: hook alias

Wp_schedule_single_event

Schedule a WordPress hook to trigger the corresponding function at the specified time. Unlike wp_schedule_event, this method is scheduled to trigger only once, and there is no loop reservation.

<?php wp_schedule_single_event($timestamp, $hook); ?>

$ Timestamp: Time (integer)
$ Args: array of parameters of the function corresponding to the hook (optional)

From the available parameters of the above functions, we can sort out the following common parameters:

Parameters

$ Timestamp

(Integer) (required) the time at which the scheduled task is executed for the first time. a timestamp must be sent. Generally, the scheduled task is executed on the spot, but the time () function cannot be used, instead, use the WordPress time function current_time ().

Default value: None

$ Recurrence

(String) (required) execution frequency. The interval of execution. You can enter hourly (executed every hour), twicedaily (executed twice a day, that is, once every 12 hours), and daily (executed once every 24 hours ).

Default value: None

$ Hook

(String) (required. This hook is called when a scheduled task is executed. you can call this function by hanging it on the function.
Default value: None

$ Args

(Array) (optional) parameters are passed to the function mounted to the timer hook.

Default value: None

Return value

(Boolean | null) if the value is successfully added, null is returned. if the value is unsuccessful, False is returned.

Example

if( !wp_next_scheduled( 'test' ) ) wp_schedule_event( current_time( 'timestamp' ), 'twicedaily', 'test' );

First, use the wp_next_scheduled () function to determine whether a scheduled task has been created. if not, create a scheduled task.

Mount the code to the test hook.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.