-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcron_example_wordpress.php
More file actions
56 lines (39 loc) · 1.36 KB
/
Copy pathcron_example_wordpress.php
File metadata and controls
56 lines (39 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
@package CronExamplePlugin
Plugin Name: CronExamplePlugin
Plugin URI: http://webox.com.pk
Description: Example plugin for cron in wordpress
Author: Hassan Ali
Version: 1.0
*/
// create a scheduled event (if it does not exist already)
function cronstarter_activation() {
if( !wp_next_scheduled( 'mycronjob' ) ) {
wp_schedule_event( time(), 'daily', 'mycronjob' );
}
}
// and make sure it's called whenever WordPress loads
add_action('wp', 'cronstarter_activation');
// here's the function we'd like to call with our cron job
function my_repeat_function() {
// do here what needs to be done automatically as per your schedule
// in this example we're sending an email
// components for our email
$recepients = 'you@example.com';
$subject = 'Hello from your Cron Job';
$message = 'This is a test mail sent by WordPress automatically as per your schedule.';
// let's send it
mail($recepients, $subject, $message);
}
// hook that function onto our scheduled event:
add_action ('mycronjob', 'my_repeat_function');
// unschedule event upon plugin deactivation
function cronstarter_deactivate() {
// find out when the last event was scheduled
$timestamp = wp_next_scheduled ('mycronjob');
// unschedule previous event if any
wp_unschedule_event ($timestamp, 'mycronjob');
}
register_deactivation_hook (__FILE__, 'cronstarter_deactivate');
?>