Debugging a sample of PHP scripts for timed tasks in WordPress _php instance

Source: Internet
Author: User
Generally people use the WordPress cron API to achieve timed tasks, it is the principle of the creation of scheduled tasks stored in the database, when someone visit to determine whether the time required to perform this scheduled task, if the time to execute.

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.

Want to create a scheduled task in WordPress generally with two functions on the line, one of which is wp_schedule_event ()

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

Such a timed task with this method is turned on and will be executed until the scheduled task is manually shut down.

When you start a scheduled task, it is difficult to debug because the scheduled task does not run every time the page is refreshed. In this case, the code errors and bugs are hard to find.

There is a way to help you with debugging. This method is to access http://your domain name. Com/wp-cron.php?doing_wp_cron, all scheduled tasks will be executed once during the visit, so it is easy to debug.

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

Turn off timed tasks

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

/**  *wordpress Disable timed task  *http://www.endskin.com/debug-cron/*/define (' Disable_wp_cron ', true);

Adjust the frequency of execution

Many people say that timed tasks have a very annoying design, that is, only three task execution frequency can be set, namely hourly (executed hourly), twicedaily (executed two times per day, that is, 12 hours of execution) and daily (24 hours of execution), If you want other execution frequency there is no way.

That WordPress really does not support the custom scheduled task execution frequency? The answer is no, WordPress supports custom timed task execution frequency, but more cumbersome, need to use hook modification.

The code below I added a new task execution frequency called weekly, executed once a week:

function Bing_add_schedules ($schedules) {  $schedules [' weekly '] = Array (    ' interval ' = = 604800,//execution frequency in seconds    ' Display ' + __ (' Once a week ')//displayed on the front-end name  );  return $schedules;} Add_filter (' cron_schedules ', ' bing_add_schedules ');

The execution frequency of this creation can then be used on the Wp_schedule_event () function:

Wp_schedule_event (current_time (' timestamp '), ' weekly ', ' Test ');
Summary: With Cron_schedules hooks You can add more execution frequency to achieve the goal of custom timed task execution frequency.

  • Related Article

    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.