1

I am using wp_schedule_event to hourly insert items from an rss news feed into my client's blog. Here is how I am setting it up in my functions.php:

add_action('init', function(){
     $timescheduled = wp_next_scheduled('update_feed');
     wp_unschedule_event($timescheduled, 'update_feed');

    if (!wp_next_scheduled('update_feed'))
    wp_schedule_event(time(), 'hourly', 'update_feed');
});

add_action('update_feed', 'update_fashion_news');

/* rest of the code for updaring feed goes here */

So first I unschedule it and then schedule the event. Now this seems to work some days and other times it doesn't. Is the unscheduling part necessary? Am I doing this right? What I want to know is:

  1. Am I using my function calls in the right order?
  2. Are the schedule and unschedule calls cancelling each other out?

Here is the full code in a gist if you want to see it: https://gist.github.com/3984658

2 Answers 2

1

Inspect your Schedules

The best way to learn coding is to turn debug on and simply inspect your output. If you want to dig deeper, then you often need a custom tool that allows you to inspect the result fast and nicely, readable formatted.

I wrote a plugin for some other answer, that helps you inspecting the WP "Cron Jobs"/Schedules. You can grab it on GitHub as Gist.

Here's what the output looks like.

enter image description here

Forks

@StephenHarris forked the plugin, which is also available as Gist.

Primary difference between this plug-in and the original, is that this displays each seperate cron-job even when two or more share the same hook (and arguments).

- Stephen Harris in the plugins comment

3
  • Thanks @kaiser. I'll try this out for debugging but could you tell me if I am using my function calls for schedule and unschedule in the right order? Or is one cancelling the other out? Commented Oct 31, 2012 at 4:40
  • Question @StephenHarris: If I install this gist as a plugin, where exactly would I see the output in the admin panel? Commented Oct 31, 2012 at 4:45
  • @AmitErandole You would see the output of both plugins in the shutdown hook, which is the last hook in theme and admin requests.
    – kaiser
    Commented Oct 31, 2012 at 14:05
1

I second Kaiser as you need to do the check, BUT also, check the visitors pattern to the site. The word press cron scheduler is triggered by visits, so, if your site is not receiving regular visitors around the clock it will miss schedules and the cron will not run.

Hope this helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.