1

I customized a calendar plugin which shows today's birthdays and current month's list of wedding anniversaries in the home page of the site. I wrote a code in that plugin's displaying page using wp_mail and mail will send. But this happens only when the site is visited. My code:

if($dat==date('Y-m-d'))/*$dat is the date of event from DB*/
 {
 if($eid!=''){ /*if recipient email id is not null*/
 if($se!=1)  /*if email is sending first time then($se=db column 'send'value) $se=0 otherwise it is 1*/
 {
 $to=$eid;
 $sub="Birthday Wishes";
  $msg='Happy Birthday '.$ev_title[$j];
 $headers= 'From:Mysite <[email protected]>' . "\r\n".'Content-type: text/html'; 
  $attachments=array(WP_CONTENT_DIR . '/plugins/spider-event-calendar/images/happybday.gif');
  $rx=wp_mail($to,$sub,$msg,$headers,$attachments);
  $wpdb->update($wpdb->prefix.  "spidercalendar_event",array('send'=>1),array('id'=>$ev_id[$j]));/**/
   //echo "email send";
   }
  else{
      //echo "email already sent";
    }
  }
 }

I heard about wp_cron but when I searched in this forum about how to write cron in WordPress I saw an answer like

Unfortunately the WordPress cron jobs are only triggered when your site is visited

If it is true then how can I send emails daily even without visiting the webpage? Is there any other way for this?

2
  • This answer offers an extended explanation - and some links - on how WP Cron works. Additionally, the wordpress codex has an overview of WP-Cron Function. This excellent answer covers both working with WP Cron and Unix Cron and should get you started. I'm sure there are more resources on here, you just have to make use of the search to find them. Commented Apr 9, 2014 at 10:19
  • thank you ialocin..i will go through all the links u shared..
    – Zammuuz
    Commented Apr 9, 2014 at 10:21

1 Answer 1

2

Unfortunately the WordPress cron jobs are only triggered when your site is visited

This is true.

The only way to reliably execute code (even if visitors don't visit your site) is to use the unix cron functions.

You can just setup a unix cron job that will do a wget on your homepage.

Edit:

You can read this article to figure out how to setup unix cron job.

Just setup the usual wp_cron and schedule it to run at what ever time you want to run it.

After that setup a unix cron job to do a wget on your homepage. This will trigger a page view which in turn will trigger the wp_cron job.

2
  • can you please share how to do unix cron for the above function? i'm just started to work on wordpress and crons thats y i am asking these many doubts.. plz help me
    – Zammuuz
    Commented Apr 9, 2014 at 10:02
  • Updated my answer.
    – Sudar
    Commented Apr 9, 2014 at 11:03

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.