• No results found

Ben

N/A
N/A
Protected

Academic year: 2021

Share "Ben"

Copied!
18
0
0

Loading.... (view fulltext now)

Full text

(1)

How to use WP-Cron

Ben Lobaugh

@benlobaugh

(2)

Avid sailor Washington native Currently reside in Seattle 1965 VW Bus Pilot Head of Development at FreshMuse Seattle WordPress Meetup & WordCamp Organizer

(3)

Session Overview

What is WP-Cron and why use it? Simple testing of your code

Understanding WP-Cron intervals How to schedule a recurring task

Unscheduling tasks

Viewing scheduled tasks

How to hook WP-Cron into the system cron

(4)

What is cron?

Cron is a time-based task scheduling system commonly available on UNIX system. There is also a Windows

version

• Cron runs continuously while the OS is on

• Tasks are scheduled to run at specific times. I.E.

Every hour at 5 minutes past the hour.

• Tasks may be missed if the system (or cron) is not

(5)

What is WP-Cron?

WP-Cron is the WordPress time-based task scheduler. WP-Cron runs on each page load and looks for tasks

(6)

Pros of WP-Cron

• Already integrated with WordPress and has a simple

API

• Many hosts (shared) do not allow access to the

system cron. This method still allows for time-based execution of tasks.

• Can be hooked into system cron for a more realistic

cron experience

(7)

Cons of WP-Cron

• Does not run continuously, only on page load

• WordPress makes an HTTP call to wp-cron.php. Some

server firewalls may block this type of external HTTP request

• High traffic sites may see WP-Cron using lots of resources.

This is a case where hooking WP-Cron to the system cron is a good idea

• Site caching may prevent WP-Cron from running until the

(8)

Understanding intervals

Unlike a traditional cron, WP-Cron does not schedule tasks for specific times. Instead, WP-Cron is given a first execution time and an interval in seconds to

execute the task again.

Example: A task scheduled for June 2nd, 2012 at 2:00 PM with a 300 second interval will fire at 2:00, 2:05, 2:10, etc

(9)

Built-in WP-Cron intervals

By default WP-Cron comes with only 3 built-in intervals

• hourly

• twice daily • daily

(10)

Adding additional intervals

In true WordPress fashion, there is a hook to add new intervals to WP-Cron. This hook is the ‘cron_schedules` filter.

{

add_filter( 'cron_schedules', 'bl_add_cron_intervals' );

}

function bl_add_cron_intervals( $schedules ) {

$schedules['5seconds'] = array( 'interval' => 5,

'display' => __('Every 5 Seconds') );

(11)

Schedule a recurring task

The custom hook

When WP-Cron executes a task it does so by calling a

custom hook. You define this hook. Creating a new hook is simple and requires 2 parameters:

• Hook name

• Function to call

}

}

(12)

Schedule a recurring task

Check the schedule

It is very important to check if an event is scheduled

before scheduling it again. Failure to do so may result in hundreds of the same task scheduled!

wp_next_scheduled() takes the task name as a

parameter and returns false if not scheduled, or the timestamp of the next execution

(13)

Schedule a recurring task

Adding the task

Adding the new task is as simple as calling

wp_schedule_event() with the following 3 parameters:

• $timestamp - UNIX timestamp of the first execution

• $recurrence - Name of the interval (NOTE: Name, NOT

a time)

• $hook - Name of the custom hook to call

(14)

Unscheduling tasks

When a task is no longer needed it should be unscheduled. Please always unschedule tasks on plugin deactivation.

wp_unschedule_event() unschedules a task and all future

occurences from a given execution timestamp. The following two parameters are required:

• $timestamp - Timestamp of next task occurrence • $hook - Name of the custom hook for the task

(15)

View Scheduled Tasks

• Zack Tollman (@tollmanz) has created a great Debug Bar

(16)

If you want a more true cron-like experience (or freed up server resources), WP-Cron can be hooked into the system cron with the following steps:

• Disable WP-Cron execution on page load in wp-config.php

with

• Setup your system task scheduler and use a program such

as wget to call your wp-cron.php file with a command such

Hooking WP-Cron to system cron

}

}

}

define('DISABLE_WP_CRON', true);

(17)

Live Code Demo

Any questions before the demo?

Code for the demo, along with a full tutorial, can be found at

(18)

Questions?

Ben Lobaugh

@benlobaugh

References

Related documents

This Technical Standards of Good Practice considers the changing circumstances in which Guidance and Counseling is now being delivered – changes in the range of issues and levels

Next please select the device you want to set this CRON entry to. By default LONE-TAR pulls your default device, you can answer yes to take the default or select no to choose

To view crontab entries of other Linux users, login to root and use -u {username} -l as shown below.. How to Edit

8141 or 8142 Install the fresh air ventilator to the return side of the HVAC system and wire the control to turn on the system’s fan with ventilation to ensure good air mixing in

The InStyler’s rotating heated polishing barrel gives your hair the heat it needs to help style and straighten – and because the barrel is constantly moving, it won’t “press

If she doesn’t know something then it is a good time to explain it or at least write it down in the learning journal what needs to be taught soon.. Of course, there are

Biodistribution and pharmacological studies in mice showed high activity and specific binding of the original and improved [ 11 C]KR31173 radioligand in mouse adrenals

Since the theory has the premise of shifting “…the security discussion away from an exclusive focus on states and toward the welfare of individual human