Advertisement
geminilabs

[site-reviews] send email to author (or to a custom email) after review is approved

Jan 4th, 2019 (edited)
612
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Sends an email to the review author (or a custom email) when their review has been approved.
  3.  * Paste this in your active theme's functions.php file.
  4.  *
  5.  * @param string $oldStatus
  6.  * @param string $newStatus
  7.  * @param \WP_Post $post
  8.  * @return void
  9.  * @action transition_post_status
  10.  */
  11. add_action('transition_post_status', function ($newStatus, $oldStatus, $post) {
  12.     if (in_array($oldStatus, ['new', $newStatus])
  13.         || 'site-review' != $post->post_type
  14.         || 'publish' != $post->post_status) {
  15.         return;
  16.     }
  17.     $review = apply_filters('glsr_get_review', null, $post->ID);
  18.  
  19.     // Start: change these values as needed
  20.     $email = $review->email;
  21.     $subject = "Your review has been published!";
  22.     $message = "Thank you so much for your review! Just letting you know that it's now live on our website.";
  23.     // End
  24.  
  25.     $metaKey = '_sent_approved_notification';
  26.     $alreadySentNotification = get_post_meta($post->ID, $metaKey, true);
  27.  
  28.     if (!empty($email) && !$alreadySentNotification) {
  29.         if ($sent = wp_mail($email, $subject, $message)) {
  30.             update_post_meta($post->ID, $metaKey, true);
  31.         } else {
  32.             apply_filters('glsr_log', null, 'The notification to <'.$email.'> was not sent. Please verify that your server is able to send emails correctly through WordPress.');
  33.         }
  34.     }
  35. }, 10, 3);
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement