Use the Wp-cron plugin in WordPress to set _php instances of timed tasks

Source: Internet
Author: User
PHP itself is unable to create timed tasks, but WordPress comes with a pseudo-timer task (Cron) API, very easy to use, including WordPress itself, the timing of publishing articles are dependent on this API

What is WP Cron? is a WordPress set of timing trigger mechanism, you can cycle to schedule task execution. Such as: the timely release of new articles, regular detection version and other functions are through this to achieve.

What can WP Cron accomplish for us? We can cycle the update and submit the website data, the festival regularly sends the reader the greeting card or the form ...


The principle is to store the scheduled task created in the database, and when someone accesses it, it is time to determine if the timing task will need to be performed, and if the time is done.

Because of this principle, there may be some deviations in the time of execution, but as the number of Web sites climbs and web crawlers continue to be accessed, the timing of task execution becomes more accurate.

Wp-cron efficiency is not high, but it is very convenient to use, collation of the relevant functions are used as follows.

Function

Wp_get_schedule

Get a scheduled hook via the hook alias. When successful, returns the cycle period category (hourly, twicedaily, Daily, ...) and returns false on failure.

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

$hook: Hook aliases
$args: An array of parameters for the hook corresponding function (optional)

Wp_get_schedules

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

<?php wp_get_schedules ()?>

By default, the array objects obtained by the above 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. Add the following example:

Add_filter (' cron_schedules ', ' cron_add_weekly '); function cron_add_weekly ($schedules) {//Adds once weekly to the existing schedules. $schedules [' weekly '] = Array (' inte Rval ' = 604800,//1 weeks = 60 seconds * 60 Minutes * 24 hours * 7 days ' display ' + __ (' Once Weekly ')); return $schedules;} Wp_next_scheduled

Gets the next run time of the scheduled schedule with the hook alias, returning as an integral type. Often used to determine if a predetermined arrangement has been made.

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

$hook: Hook aliases
$args: An array of parameters for the hook corresponding function (optional)

Wp_schedule_event

Schedule a WordPress hook to be scheduled by cycle, triggering the function of the hook at the scheduled time.

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

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

Wp_reschedule_event

Cycle back to schedule a WordPress hook. But I found that this method can not be used normally, Codex write very grass, if who knows clearly how to use, please inform.

Wp_unschedule_event

Cancellation of scheduled arrangements via scheduled time and hook aliases.

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

Wp_clear_scheduled_hook

Remove the scheduled hooks via the hook alias.

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

Wp_schedule_single_event

Schedule a WordPress hook to trigger a tick-corresponding function at a predetermined time. Unlike Wp_schedule_event, the method only has a single trigger, and there is no looping schedule.

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

$timestamp: Time (integral type)
$args: An array of parameters 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 time of the first execution of this scheduled task, the need to pass a timestamp, in general, is executed on the spot, but not with the time () function, but instead of Current_time ().

Default value: None

$recurrence

(string) (must) the frequency of execution. How often the time is executed. You can fill out hourly (performed once per hour), twicedaily (executed two times per day, which is 12 hours of execution) and daily (24 hours of execution).

Default value: None

$hook

(string) (must) execution of the hook. This hook is called when the timer task is executed, and the function is executed when the hook is hung on the function.
Default value: None

$args

(array) (optional) passed parameters, which are passed to the parameters of the function attached to the timed hook.

Default value: None

return value

(Boolean | NULL) returns NULL if added successfully, 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 if it has been created and 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.