1

On our WordPress website we are using the Events Manager Pro plugin. This allows us to create events. However we need to somehow send a thank you E-mail to the attending people 1 day after the event end date.

I have already written this up:

add_action( 'em_bookings_added', 'mail_after_completion' );
function mail_after_completion (){

global $wpdb;

/*Get the email of who booked */
$person_id = $wpdb->get_var("SELECT person_id FROM mbc_em_bookings ORDER BY booking_date DESC LIMIT 1");
$person_email = $wpdb->get_var( $wpdb->prepare("SELECT user_email FROM mbc_users WHERE ID = %s",$person_id));

/*Get the event finish date */
$event_id = $wpdb->get_var("SELECT event_id FROM mbc_em_bookings ORDER BY booking_date DESC LIMIT 1");
$event_end_date = $wpdb->get_var( $wpdb->prepare("SELECT event_end_date FROM mbc_em_events WHERE event_id = %s",$event_id));


/*Send E-mail to the person who just booked */


    $to = '$person_email';  
    $subject = "Thank you for attending";
    $content = "Thank you for attending the event at $event_end_date";
    $status = wp_mail($to, $subject, $content);

    }
 ?>

This function sends an E-mail to a person whenever they book an event. However I would like this to happen 1 day after the event completes. I've looked into CRON and wp_schedule_single_event() a lot but can't seem to figure out how to properly use it.

If somebody could shed some light on this it would be greatly appreciated

Kind regards!

1
  • 1
    $to = '$person_email' should be $to = $person_email, otherwise you are literally trying to send to the string $person_email (single vs double quotes) Sep 26, 2016 at 13:45

2 Answers 2

1

So the gist is that you have your function, you hook it into an action and then you tell WordPress to fire that action at some time on its schedule. The built in schedules that are available to you are:

  • hourly
  • twicedaily
  • daily

If you wanted to fire your event every hour, you would do something like this:

<?php

wp_schedule_event(time(), 'hourly', 'em_bookings_added');

If you want to get a task to run at a specific time, you can simply pass a unix timestamp as the first argument:

<?php

$future_time = mktime(10,0,0,30,9,2016); // 2016-09-30 10:00:00 UTC
wp_schedule_event($future_time, 'hourly', 'em_bookings_added');

If you have any arguments that you want to pass to your action, you can pass them as a fourth argument.

You can also specify a time that you would like your action to run -- that's the first argument. However please be aware that WordPress cron relies on people visiting the site to run and isn't as accurate as a real cron job; no visitors == no cron running.

One way around this, however, is to set a cron job on your own server to trigger wp-cron.php manually:

Taken from Tom McFarlin's awesome article on WordPress cron

// wp-config.php
define('DISABLE_WP_CRON', true);  

// in your crontab
/15 * * * wget -q -O - http://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

See more at https://codex.wordpress.org/Function_Reference/wp_schedule_event

2
  • Thank you for taking your time to explain! I understand the gist of using the CRON system. I have one more question though, is it possible to fire the function at a time in a variable. I wanted the function to fire on the date in $event_end_date which holds 2016-11-22 Sep 26, 2016 at 9:28
  • Hey, I've updated my answer to include an example for using future dates. You can use mktime()
    – FaCE
    Sep 26, 2016 at 11:25
0

With WordPress you can't trigger action at specific time. Better to use server side cron to perform action

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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