WordPress.org

Forums

wp_schedule_event events don't fire (8 posts)

  1. ashes999
    Member
    Posted 6 years ago #

    I wrote a couple of plugins. The first one, on activate, sets up an event to fire tomorrow (the next day) at midnight (plus 1 minute). Here's my code:

    function plugin_activate() {
      updateUserScores();
      wp_schedule_event(strtotime(date('Y-m-d 00:01:00', , strtotime("+1 day"))), 'daily', 'updateUserScores');
    }

    When I call wp_next_scheduled('updateUserScores') (and throw it through the PHP date function), it gives me the right timestamp. But when I visit the site, nothing happens. I don't have any sort of caching installed.

    I've tried removing the +1 day to make it TODAY at midnight (i.e. a timestamp in the past); it still doesn't fire my event. Not sure what's going on ...

  2. Austin Matzko
    Member
    Posted 6 years ago #

    Sorry to ask the obvious, but you do have a callback function for the "updateUserScores" action hook, right?

    If you do and the callback is never called, then you should check if WordPress's built-in cron is working at all. Try a couple of other plugins that use the cron functionality, or look at the "cron" option in the wp_options database table to see if scheduled pings are piling up in there and not being discharged.

  3. ashes999
    Member
    Posted 6 years ago #

    Yes, I do have the callback function :) I even scheduled a wget every five minutes so I can be sure I have timely "visitors" to trigger my cron event.

    I'm using wp-db-backup, which uses schedule-event, and it triggers fine; can you suggest another plugin?

    wp_options only has my function listed once under the cron option. However, I did see something called doing_cron which is set to 0.

    To clarify, I have a table called wp_logging which I can log messages to (it gives me the timestamp). My code is more like this:

    function activate() {
      updateScores();
      wp_schedule_event(...)
    }
    
    function updateScores
      ...
      logMessage("Scores updated.")
      logMessage("Next update: " . wp_next_scheduled(...));
    }

    I can see the initial logging message from the first call of updateScores, and it gives me the correct timestamp for wp_next_scheduled; but the updateScores function never fires after that. (The data that should be inserted by updateScores doesn't show up, either.)

  4. Are you confusing functions with actions?

    The event you've scheduled will not run a function, it will do an action. So where's your add_action() call, to hook that action to the function?

    Like this:

    function my_activation() {
    wp_schedule_event(whatever, 'daily', 'my_action');
    }
    
    function my_function() {
    ...
    }
    
    add_action('my_action','my_function');
  5. ashes999
    Member
    Posted 6 years ago #

    Otto42: I suspect you're right. Strange how I missed that one line of code when I skimmed the tutorials.

    It seems to be working now with a past-timestamp; I'm going to go ahead with my original functionality and let you know tomorrow if it works. And I anticipate that it will :)

    Is it possible we can update the documentation for wp_schedule_event to mention that you do NOT pass in a function, but rather, you need to add a hook? It's a bit unclear :/

  6. The documentation makes it pretty clear, I thought:
    http://codex.wordpress.org/Function_Reference/wp_schedule_event

    I mean, it even calls it "my_scheduled_hook" and everything. And here's the main description:

    Schedules a hook which will be executed by the WordPress actions core on a specific interval, specified by you. The action will trigger when someone visits your WordPress site, if the scheduled time has passed. See the Plugin API for a list of hooks.

    Don't see how it could be any clearer.

    Also, make certain that you do the unschedule on deactivation. Otherwise your hook hangs around forever.

    Another thing: Calling the function manually and then scheduling for 1 day in advance seems a bit of a weird thing to do, to me. Instead, you should just schedule it for now() with a daily recurrence. It'll execute then on the very next page load, which they have to do to get to your settings screen anyway.

  7. ashes999
    Member
    Posted 6 years ago #

    Maybe it could say "Actions are NOT arbitrary functions. Click [here] to learn about Actions." I didn't even know this notion of actions exists in WP.

    Anyhow, it's resolved--thanks Otto. The reason I activate and schedule is because I need it to run at a specific time (midnight), even if I activate at a different time; but running the code on activate is mostly for debugging. I had my long-term design a bit confused :)

  8. Austin Matzko
    Member
    Posted 6 years ago #

    In ashes999 defense, the inline docs do say

    @param callback $hook Function or method to call, when cron is run.

    which is wrong, and oddly is corrected on the Codex version.

    At any rate, I've made a patch.

Topic Closed

This topic has been closed to new replies.

About this Topic