Use the Wp-cron plugin in WordPress to set the timer task _php instance

Source: Internet
Author: User

PHP itself is unable to create timed tasks, but WordPress has a pseudo-timer task (Cron) API, very convenient and easy to use, including the WordPress itself regularly published articles are dependent on this API

What is WP Cron? WordPress is a set of timer trigger mechanism, you can cycle the task execution. such as: Regular release of new articles, periodic detection version and other functions are through this to achieve.

What can WP Cron achieve for us? We can cycle updates and submit Web site data, and festivals send greeting cards or forms to readers regularly ...


It is the principle of the creation of the timing of the task stored in the database, when someone visited to determine whether the time required to perform this task, if the time to execute.

Because of this principle, so the execution time may have some deviation, but as the website's browsing volume climbs and the network crawler's unceasing access, will let the timing task execution time more and more accurate.

Wp-cron efficiency is not high, but it is very convenient and easy to use, sorting out the relevant functions of the following methods.

Function

Wp_get_schedule

By Hook alias, get the hook of the predetermined arrangement. Returns the cycle category (hourly, twicedaily, daily, ...) when successful, returning false on failure.

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

$hook: Hook Alias
$args: An array of arguments for the hook corresponding function (optional)

Wp_get_schedules

The WordPress default supported cycle cycle categories are hourly, twicedaily and daily. With this function we can get all of these cycle arrays.

<?php wp_get_schedules ()?>

By default, the array objects obtained by the above methods 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. Add examples 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,//1 week = 60 sec * 60 min * 24 hours * 7 days
 ' Display ' => __ (' Once W Eekly ')
 );
 return $schedules;
}
Wp_next_scheduled

With the hook alias, get the next run-time of the scheduled schedule to return as an integer. Often used to determine whether a predetermined arrangement has been made.

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

$hook: Hook Alias
$args: An array of arguments for the hook corresponding function (optional)

Wp_schedule_event

A WordPress hook is scheduled to be arranged periodically, triggering the corresponding function of the hook at a predetermined time.

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

$timestamp: Time (integral type)
$recurrence: Cycle cycle category (hourly, twicedaily, daily, ...)
$hook: Hook Alias
$args: An array of arguments for the hook corresponding function (optional)

Wp_reschedule_event

Reschedule a WordPress hook by cycle. But I found this method is not normal use, Codex write very grass, if you know how to use, please inform.

Wp_unschedule_event

Cancel the scheduled arrangement by booking time and Hook alias.

<?php wp_unschedule_event ($timestamp, $hook, $args);?>
$timestamp: Time (integral type)
$hook: Hook Alias
$args: An array of arguments for the hook corresponding function (optional)

Wp_clear_scheduled_hook

By Hook alias, remove the hook of the predetermined arrangement.

<?php Wp_clear_scheduled_hook ($hook);?>
$hook: Hook Alias

Wp_schedule_single_event

A WordPress hook is scheduled to trigger the function of the hook at a predetermined time. Unlike Wp_schedule_event, the method is scheduled to be triggered only once and does not have a cyclic reservation.

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

$timestamp: Time (integral type)
$args: An array of arguments for the hook corresponding function (optional)

Judging from the parameters available from the above function, we can sort out the following common parameters:

Parameters

$timestamp

(integer) (must) the first time to perform this timed task, you need to pass a timestamp, in general, are executed on the spot, but can not use time () function, but with the function of WordPress current_time ().

Default value: None

$recurrence

(string) (must) the frequency of execution. How often do you perform it at every interval? You can fill in hourly (once per hour), twicedaily (two times a day, i.e. 12 hours) and daily (24 hour execution).

Default value: None

$hook

(string) (must) execute the hook. This hook is called when the timer is executed, and the hook is hung on the function to implement the timed execution function.
Default value: None

$args

(array) (optionally) passed parameters that are passed to the function that is mounted to the timer hook.

Default value: None

return value

(Boolean | NULL) returns NULL if the addition succeeds, or False if unsuccessful

Example

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

First use the wp_next_scheduled () function to determine whether you have created it, or create a timed task if it is not created.

Mount the code you want to execute on 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.