0

I'm trying to check do an action in my wp plugin each hour, but it does not work fine.

I'm doing this:

register_activation_hook(__FILE__, function() {
    //more stuff

    if (! wp_next_scheduled ( 'my_hourly_event' )) {
        wp_schedule_event(time(), 'hourly', 'my_hourly_event');
    }
});
add_action('my_hourly_event', 'do_this_hourly');

function do_this_hourly() {
    wp_mail('[email protected]', 'prueba cron', 'prueba cron innovation');
}

But I only receive 2-5 mails a day and not at the expected hours (for example, today I only received 2 emails, at 8:21am and 11:51am; yesterdday other 2 both of them at 11:45 ...).

I have no access to the server to make a real cron job.

1
  • e-mail can be lost between the server and your mailbox. then to have reliable informations of what append, save the dates in a log (e.g. a text file)
    – mmm
    Commented Sep 8, 2016 at 10:54

1 Answer 1

0

You probably don't have enough visitors on your site. Although it looks otherwise WordPress doesn't actually schedule the events. It writes the time when it should happen to the database, and only when somebody visits the site WP gets fired up, checks the database and fires the event.

So, if nobody is around at the time you want it, your event won't happen.

What you need to do in order to make sure that an event fires exactly at the time you want, is circumventing WP's own scheduling system and schedule it directly with the operating system. If you google for "wordpress force cron job" you will find plenty of tutorials. I like this one.

This, of course, won't work if you have no access to a cpanel or other hosting service that allows you to set up a cron job (as you say). In that case google for "free cron job". That will give you a server that will act as the hourly visitor that fires the event.

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.