6

In WordPress, I am creating a plugin where I am sending emails to users. For that, I am using WordPress cron job. So basically what it will do is just send emails to users every hour. So my code looks like this

public function __construct() {
    add_action('init', array( $this, 'send_emails_to_users') );  
    add_action('cliv_recurring_cron_job', array( $this, 'send_email') );
}

public function send_emails_to_users() {
  if(!wp_next_scheduled('cliv_recurring_cron_job')) {
          wp_schedule_event (time(), 'hourly', 'cliv_recurring_cron_job');
    }
}

public function send_email() {
    //send email code goes here
}

Here everything looks good but it does not send the email.

If I make my code like this

public function __construct() {
    add_action('head', array( $this, 'send_email') );  
}

Then it sends the email. But the problem is here it sends the email on every time the page loads or when the user visits the site.

That's why I want to use wp_schedule_event to make emails every hour.

So can someone tell me how to resolve this issue?

Any suggestions or help will be really appreciated.

Nmk
  • 1,281
  • 2
  • 14
  • 25
NewUser
  • 12,713
  • 39
  • 142
  • 236
  • have you got a server cron set up? wp will only send when someone visits the site and the task is due – David Mar 30 '16 at 19:49
  • I test yours is work. For test you need to test interval just in seconds. – Jevuska Mar 30 '16 at 21:44
  • @David I am testing it on my local LAMP – NewUser Mar 31 '16 at 02:17
  • @Jevuska I am not getting you. – NewUser Mar 31 '16 at 02:18
  • 4
    wordpress cron is not a cronjob (as in server) it does not have the capacity to run itself, what happens is when someone visits the site, they trigger off whatever wp cronjobs are due. This differs from a server (unix = crontab, windows = task scheduler) cron job that will actually run the code at the correct time. The way to do this with wp is to set up your job as above and then create a crontab to visit your site just after the scheduled time. – David Apr 01 '16 at 14:21
  • I need you clear your event by [`wp_clear_scheduled_hook`](https://codex.wordpress.org/Function_Reference/wp_clear_scheduled_hook), since it may added under database `wp_options` with `option_name` is `cron`.Also you can check by `get_option('cron');`. If your event is not there, you can run yours just in seconds, say just for 5 seconds, so you don't have to wait for hours :). Then test your cron like this example.com/wp-cron.php after pass time. Please see [this](http://ben.lobaugh.net/blog/20787/wordpress-how-to-use-wp-cron) Ben tutorial how you setup your cron inseconds and test. – Jevuska Apr 06 '16 at 17:16
  • @Jevuska thanks for this url. But can you tell me how can I run my function without visiting wp-cron page. As because my function is inside the plugin so how users will access that function by visiting the site? – NewUser Apr 07 '16 at 03:08
  • see @David comment, your function will be fire automatically by first visitor ( if time pass ) then re-scheduling your event. I show you to visit wp-cron just for Test if your function work or not after you make sure your even in wp-option. – Jevuska Apr 07 '16 at 03:33
  • @Jevuska then what about this as per @David comment ? `create a crontab to visit your site just after the scheduled time` – NewUser Apr 07 '16 at 11:33
  • 1
    @newUser you are missing the point......wp_schedule_event exists in your db and php app (wp). There is no method to start php itself. When you visit a wp app and a scheduled evt is overdue, php will run the code attached to the event. If no-one visits, it does run. Now linux "Cronjob" is different altogether, it runs at the spec. time because it is a linux app, if server on, it runs at the correct time. You know the email is being sent, so your code works. Crontab you test on the prod server (it mostly works anyway). http://stackoverflow.com/questions/7195503/setting-up-a-cron-job-in-windows – David Apr 07 '16 at 21:59
  • ran out of space :( - the way to ensure your wp evt fires on time is to simulate a visit to your site using cron (there are a few different ones available in windows) but i don't think you need to test locally, once the email is being sent when you visit manually (check server time settings if not), it will work when crontab visits the site. On your live server, yes test it! https://www.siteground.com/tutorials/wordpress/setup-cron-job.htm – David Apr 07 '16 at 22:06
  • Install this plugin to view and make sure your WP cron has been registered first: https://wordpress.org/plugins/cron-view/ – Hyder B. Apr 11 '16 at 16:56

2 Answers2

0

First of all , 1) You need to setup crontab in your server if you want to work dynamically 2) if you want manually wordpress scheduler will call after the page is run

so,

for the crontab setup below is useful link: crontab

Pranav Bhatt
  • 715
  • 4
  • 8
0

If you want to run your cron in every one hour then you need to add below code:

public function __construct() {
    // Call function for cron
    add_action('init', array( $this, 'send_emails_to_users') );
}

public function send_emails_to_users() {
    if(!wp_next_scheduled('cliv_recurring_cron_job')) {
        // Add "cliv_recurring_cron_job" action so it fire every hour
        wp_schedule_event(time(), 'hourly', 'cliv_recurring_cron_job');
    }
}

add_action('cliv_recurring_cron_job', array( $this, 'send_email') );
public function send_email() {
    //send email code goes here
}

for more information see link

Mukesh Panchal
  • 1,956
  • 2
  • 18
  • 31
  • Yes I have done my code like that. But I want to know should I load wp-cron.php for running the cron or even I load any page on my site will run the custom function? – NewUser Apr 13 '16 at 12:19
  • when WordPress init it automatically call no need to load wp-cron.php, check init action here https://codex.wordpress.org/Plugin_API/Action_Reference/init – Mukesh Panchal Apr 13 '16 at 12:32
  • then what about run the function when there is no user visits the site. I want to run my function even there is no user visiting the site. – NewUser Apr 13 '16 at 14:07
  • So you need to add one php page in root of wordrpess directory and add cron job in server so it auto run every 1hr – Mukesh Panchal Apr 14 '16 at 04:10