Debug related PHP script examples of scheduled tasks in WordPress

Source: Internet
Author: User
This article mainly introduces the PHP script examples for debugging scheduled tasks in WordPress. for scheduled tasks written using CronAPI and the wp_schedule_event () function, for more information about how to use the Cron API of WordPress to implement scheduled tasks, you can store the created scheduled tasks in the database, when someone accesses the task, it determines whether the scheduled task needs to be executed by time, and if so, it will be 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.

To create a scheduled task in WordPress, you can use two functions, one of which is wp_schedule_event ()

wp_schedule_event( $timestamp, $recurrence, $hook, $args );

If such a scheduled task is enabled, it will be executed until the scheduled task is disabled manually.

It is difficult to debug a scheduled task after you start a scheduled task because it is not executed every time you refresh the page. In this case, it is difficult to find code errors and bugs.

There is a way to help you debug. This method is to access http: // your domain name. com/wp-cron.php? Doing_wp_cron: all scheduled tasks are executed once during access, which facilitates debugging.

http://www.example.com/wp-cron.php?doing_wp_cron

Disable scheduled tasks

If you want to disable scheduled tasks, you can add the following code in the wp-config.php:

/*** WordPress disables a scheduled task * http://www.endskin.com/debug-cron/#/define ('Disable _ wp_cron', true );

Adjust execution frequency

Many people say that scheduled tasks have a very annoying design, that is, they can only set three task execution frequencies, namely, hourly (once every hour) and twicedaily (twice every day, that is, the execution is performed once every 12 hours) and the daily (once every 24 hours). If you want other execution frequencies, there is no way.

Does WordPress really not support the execution frequency of custom scheduled tasks? The answer is No. WordPress supports customizing the execution frequency of scheduled tasks, but it is troublesome and requires hook modification.

The following code adds a new task execution frequency called weekly, which is executed once a week:

Function Bing_add_schedules ($ schedules) {$ schedules ['weekly '] = array ('interval' => 604800, // execution frequency in seconds 'display' => _ ('once a week') // display the name of the front end); return $ schedules ;} add_filter ('cron _ schedules ', 'Bing _ add_schedules ');

Then, the execution frequency created can be used in the wp_schedule_event () function:

Wp_schedule_event (current_time ('timestamp'), 'weekly ', 'test ');
Conclusion: you can use the cron_schedules hook to add more execution frequencies to customize the execution frequency of scheduled tasks.

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.