{"id":1908,"date":"2009-12-14T08:35:47","date_gmt":"2009-12-14T07:35:47","guid":{"rendered":"http:\/\/wpengineer.com\/?p=1908"},"modified":"2013-09-24T14:01:58","modified_gmt":"2013-09-24T12:01:58","slug":"use-wordpress-cron","status":"publish","type":"post","link":"https:\/\/wpengineer.com\/1908\/use-wordpress-cron\/","title":{"rendered":"Use WordPress Cron"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wpengineer.com\/wp-content\/uploads\/WordPress-Christmas-14.jpg\" alt=\"WordPress-Christmas-14\" title=\"WordPress-Christmas-14\" width=\"600\" height=\"400\" class=\"aligncenter size-full wp-image-1892\" \/>WordPress has its own cron to automatically and scheduled run certain themes. Therefore WordPress provides <a title=\"read in codex\" href=\"http:\/\/codex.wordpress.org\/Function_Reference\/wp_schedule_event\">several functions<\/a> to use the cron.<\/p>\n<p>In our first example we send every hour a mail with the help of the WordPress function <code>wp_mail()<\/code>. FYI, this is just a possibility, please don&#8217;t do it on your system!<\/p>\n<p>As default, WordPress can handle 3 time keys, which you can call with the function <code>wp_schedule_event<\/code>.<\/p>\n<pre lang=\"php\">\r\n\/\/ send automatic scheduled email\r\nif ( ! wp_next_scheduled('my_task_hook') ) {\r\n\twp_schedule_event( time(), 'hourly', 'my_task_hook' ); \/\/ hourly, daily and twicedaily\r\n}\r\n\r\nadd_action( 'my_task_hook', 'my_task_function' );\r\nfunction my_task_function() {\r\n\t\r\n\twp_mail( \r\n\t\t'example@yoursite.com', \r\n\t\t'Automatic mail', \r\n\t\t'Hello, this is an automatically scheduled email from WordPress.'\r\n\t);\r\n}\r\n<\/pre>\n<p>If you use the cron in a Plugin or theme, then don&#8217;t forget to deactivate the cron if you don&#8217;t need it anymore.<\/p>\n<pre lang=\"php\">\r\ndelete_action( 'my_task_hook', 'my_task_deactivate' );\r\n\/\/ clean the scheduler\r\nfunction my_task_deactivate() {\r\n\t\r\n\twp_clear_scheduled_hook( 'my_task_hook' );\r\n}\r\n<\/pre>\n<p>But not always are 3 time values enough. Luckily you can expand the control via a filter.<\/p>\n<pre lang=\"php\">\r\nadd_filter( 'cron_schedules', 'filter_cron_schedules' );\r\n\/\/ add custom time to cron\r\nfunction filter_cron_schedules( $schedules ) {\r\n\t\r\n\t$schedules['once_half_hour'] = array( \r\n\t\t'interval' => 1800, \/\/ seconds\r\n\t\t'display'  => __( 'Once Half an Hour' ) \r\n\t);\r\n\t\r\n\treturn $schedules;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>WordPress has its own cron to automatically and scheduled run certain themes. Therefore WordPress provides several functions to use the cron. In our first example we send every hour a mail with the help of the WordPress function wp_mail(). FYI, this is just a possibility, please don&#8217;t do it on your system! As default, WordPress [&hellip;]<\/p>\n","protected":false},"author":321,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[172,28,17,3,272,20],"class_list":["post-1908","post","type-post","status-publish","format-standard","hentry","category-wordpress-tutorials","tag-advent-calendar","tag-code","tag-php","tag-wordpress","tag-wordpress-hacks","tag-wp"],"_links":{"self":[{"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/posts\/1908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/comments?post=1908"}],"version-history":[{"count":0,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/posts\/1908\/revisions"}],"wp:attachment":[{"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/media?parent=1908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/categories?post=1908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/tags?post=1908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}