{"id":2807,"date":"2020-05-14T10:20:02","date_gmt":"2020-05-14T09:20:02","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=2807"},"modified":"2022-02-17T07:13:48","modified_gmt":"2022-02-17T07:13:48","slug":"creating-cron-function","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/wordpress\/cron-scheduled-tasks\/creating-cron-function","title":{"rendered":"Creating cron function"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Setting Up cron within wordpress<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Using standard  recurrence values <\/h5>\n\n\n\n<p>Standard recurrence values are &#8216;hourly&#8217;, &#8216;daily&#8217; and &#8216;twicedaily&#8217;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/******************************************************************************\n\/\/******************************************************************************\n\/\/********** REGISTER \/ DE-REGISTER OUR CRON FUNCTIONS WITH WORDPRESS **********\n\/\/******************************************************************************\n\/\/******************************************************************************\n\/\/***** ACTIVATE *****\nfunction my_cron_events_activate() {\n\n  \/\/Deactive first to ensure there won't be repeated events\n  my_cron_events_deactivate();\n\n  $Result = wp_schedule_event( time(), 'hourly', 'my_cron_main_event' );  \/\/&lt;&lt;&lt;Namespace only needed if outside of the current namespace\n\n\n\n  \/*\n  if (is_wp_error($Result))\n    echo \"wp_schedule_event error: \" . $Result-&gt;get_error_message();\n  else if ($Result === True)\n    echo \"wp_schedule_event success\";\n  else\n    echo \"wp_schedule_event something went wrong\";\n  *\/\n}\n\n\/\/***** DEACTIVATE *****\nfunction my_cron_events_deactivate() {\n    wp_clear_scheduled_hook('my_cron_main_event');\n\n  \/\/&lt;&lt;&lt;Namespace only needed if outside of the current namespace\n\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Using your own recurrence values<\/h5>\n\n\n\n<p>If you want something different from the standard recurrence values you can add the below to define them:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n  \/\/Using in your activate function:\n  $Result = wp_schedule_event( time(), '5min', 'my_cron_main_event' );\n\n\n\n\/\/*************************************************\n\/\/***** CREATE OUR OWN CUSTOM SCHEDULE VALUES *****\n\/\/*************************************************\nadd_filter('cron_schedules','my_cron_schedules');    \/\/&lt;&lt;&lt;Change my_cron_schedules to a unique name (can't be shared by plugins or themes).\n\/\/add_filter('cron_schedules','\\MyNamespaceName\\my_cron_schedules');\nfunction my_cron_schedules($schedules)\n{\n  \/\/Add our custom schedule times here (1 or more)\n  if(!isset($schedules&#91;\"5min\"]))\n  {\n      $schedules&#91;\"5min\"] = array(\n          'interval' => 5*60,\n          'display' => __('Once every 5 minutes'));\n  }\n  \n  \/*\n  if(!isset($schedules&#91;\"30min\"]))\n  {\n      $schedules&#91;\"30min\"] = array(\n          'interval' => 30*60,\n          'display' => __('Once every 30 minutes'));\n  }\n  *\/\n  \n  return $schedules;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Configuring WordPress to use your setup<\/h4>\n\n\n\n<p>Run this once to setup with the new configuration<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/Recreate all events\n  my_cron_events_activate();\n\n\n\n  \/\/Delete all events\n  my_cron_events_deactivate();<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Using with a plugin<\/h5>\n\n\n\n<p>Use the wp_schedule_event() with your plugin activation hook register_activation_hook().<\/p>\n\n\n\n<p>Ensure you also use your plugins register_deactivation_hook() to clear the scheduled event as deactivating a plugin doesn&#8217;t do it.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Add a cron schedule task to your server to call the wp cron<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/    *\/5 * * * * wget -q -O - https:\/\/mydomainame.uk\/wp-cron.php?doing_wp_cron<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Your cron function<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/***********************************\n\/\/***********************************\n\/\/********** CRON FUNCTION **********\n\/\/***********************************\n\/\/***********************************\nadd_action( 'my_cron_main_event',  'my_cron_main_event' );    \/\/&lt;&lt;&lt;Change my_cron_main_event to a unique action name (can't be shared by plugins or themes).\n\/\/add_action( 'my_cron_main_event',  '\\MyNamespaceName\\my_cron_main_event' );\nfunction my_cron_main_event()\n{\n\n\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Testing \/ Debugging<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Run just your cron fucntion from code<\/h5>\n\n\n\n<p>Your cron function is created as an action, so you can trigger it to run from your own code using <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>do_action( 'my_cron_main_event' );<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Run all wp cron functions<\/h5>\n\n\n\n<p>You can do this from a browser using:<br>https:\/\/mydomainname.com\/wp-cron.php?doing_wp_cron<\/p>\n\n\n\n<p><em>Remember though that wp will only trigger your function at the time interval you set for it, regardless of how often you trigger it<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setting Up cron within wordpress Using standard recurrence values Standard recurrence values are &#8216;hourly&#8217;, &#8216;daily&#8217; and &#8216;twicedaily&#8217;. Using your own recurrence values If you want something different from the standard recurrence values you can add the below to define them: Configuring WordPress to use your setup Run this once to setup with the new configuration [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189],"tags":[],"class_list":["post-2807","post","type-post","status-publish","format-standard","hentry","category-cron-scheduled-tasks"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=2807"}],"version-history":[{"count":15,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2807\/revisions"}],"predecessor-version":[{"id":3838,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2807\/revisions\/3838"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=2807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=2807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=2807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}