1

I am new with WordPress development and using cron job for first time. I am having issue with cron job which is not firing. Here are all steps i have taken to achieve this. I have put this line in my wp-config file

define('DISABLE_WP_CRON', true);

Then i have setup a cron job in my cpanel with this command.

wget -q -O - http://mywebsite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

I have select interval for every 30 minutes.

But now cron job not firing my WordPress function. Here is my code that i am using in functions.php of my theme.

wp_schedule_event(time(), 'hourly', 'my_hourly_event'); 
add_action('my_hourly_event', 'do_this_hourly');
function do_this_hourly() {
wp_mail( 'myemailaddress@gmail.com', 'Automatic email', 'Automatic scheduled email from WordPress to test cron');
}

Can anybody tell me what i am doing wrong with this because i am not receiving emails from this function. I will really appreciate your help.
Thank you!

  • I think the system cron is firing but you are rescheduling the hourly event on every page load. I would remove >/dev/null 2>&1 from de command to check if the system cron is fired or not and then isolated the problem to the PHP code. – cybmeta Oct 3 '17 at 7:33
  • Yes it is a good idea. I have already remove this from command to test system cron job. Can you see any issue with WordPress function ? – wplearner Oct 3 '17 at 7:36
  • You are right. I think system cron is not firing. I am using this command with interval of 5 minutes but i am not receiving email from system. */5 * * * * wget -q -O - mydomain/wp-cron.php?doing_wp_cron – wplearner Oct 3 '17 at 7:50
  • You were right because there was an issue with system cron and now i am using following command and this is working for me. cd /home/user/public_html; /usr/local/bin/php -q wp-cron.php – wplearner Oct 3 '17 at 9:03
  • 1
    @cybmeta Thank you so much for explaining this very well. I am lot more clear about this. – wplearner Oct 4 '17 at 8:41
0

You need to first create the interval for 30 mins. Use filter:

add_filter('cron_schedules','my_cron_schedules', 999 );
function my_cron_schedules($schedules) {
    $schedules['thirty_min'] = array(
        'interval' => 1800, // Every 30 mins
        'display'  => __( 'Every 30 mins' ),
    );
    return $schedules;
}

Then you need to execute the scheduled job:

wp_schedule_event( time(), 'thirty_min', 'your_event_hook' );

Then add the action and the callback function:

add_action('your_event_hook', 'do_this_hourly');
function do_this_hourly() {
    wp_mail( 'myemailaddress@gmail.com', 'Automatic email', 'Automatic scheduled email from WordPress to test cron');
}

you need define('DISABLE_WP_CRON', true); in your wp-config.php

Then you can schedule your system cron.

  • Thank you so much for your detail answer. I am going to try this code for setting interval which is 30 minutes in my case. – wplearner Oct 3 '17 at 7:42
  • Did it work for you? – Annapurna Oct 3 '17 at 8:53
  • Sorry i did not test your code yet. But i figure out the issue. There was problem with system cron and now i am using different command and that is working for me. cd /home/user/public_html; /usr/local/bin/php -q wp-cron.php – wplearner Oct 3 '17 at 9:00
  • Now i will test your code and i hope it will work. Then i will accept your answer. Thank you so much for your help and time. – wplearner Oct 3 '17 at 9:02
  • All the best :) – Annapurna Oct 3 '17 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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