Include assigned post in email reply?
-
Hi – I email the reviewer when I reply to their review. Looking to include in the email a link back to the post where they left the review. Is this possible please? I understand it would be the assigned post of the review, just not sure how to access that via PHP?
Thanks so much,
Mark
-
You can get the assigned_post IDs from the review object like this:
$postIds = $review->assigned_posts
Hi – thanks for the response. So for a given review I understand there would only ever be one assigned post (certainly I cant see a scenario in my case where there would be more). I’ve tried using the above code snippet to retrieve the URL of the post the review was added to, however no joy! Code below, any ideas please? Cheers M.
/**
* Send an email notification to the review author after responding to a review
* This snippet assumes that your review form includes the email field
* Paste this code in your theme’s functions.php file.
* @param \GeminiLabs\SiteReviews\Review $review
* @param string $response
* @return void
*/
add_action(‘site-reviews/review/responded’, function ($review, $response) {
$hasResponse = !empty($review->response(1));
$hasResponseUserId = !empty(get_post_meta($review->ID, ‘_response_by’, true));
if (empty($response) || $hasResponse || $hasResponseUserId) {
return; // only send an email if the response is not empty and there is no previous response
}$posturl = $review->assigned_posts();
$wbpermalink = ‘<html><Head></Head><Body>Letting you know we have posted a reply to your review at</html>’;//MG
$email = glsr(‘Modules\Email’)->compose([
‘to’ => $review->email,
‘subject’ => ‘[My Website] We have responded to your review!’,
//’message’ => $response,
‘message’ => $wbpermalink,‘headers’ => ‘Content-Type: text/html; charset=”UTF-8″‘, //MG
]);
$email->send();
}, 10, 2);`Please wrap pasted code in code tags.
Since you can assign reviews to multiple posts,
$review->assigned_posts
will always be an array value (of 0 or more post IDs), and$review->assignedPosts()
will always be an array value (of 0 or more $post objects).Also, do not wrap your message in
<html>
or<body>
tags, and you don’t need to set the header if using the Email class.-
This reply was modified 3 years, 8 months ago by
Gemini Labs.
Sure, here we go. I think what you are saying is I would need to pass in the ID of the post, to get the URL? The code in question: $posturl = $review->assigned_posts($review->ID); //return URL of post
<?php
/**
* Send an email notification to the review author after responding to a review
* This snippet assumes that your review form includes the email field
* Paste this code in your theme’s functions.php file.
* @param \GeminiLabs\SiteReviews\Review $review
* @param string $response
* @return void
*/
add_action(‘site-reviews/review/responded’, function ($review, $response) {
$hasResponse = !empty($review->response(1));
$hasResponseUserId = !empty(get_post_meta($review->ID, ‘_response_by’, true));
if (empty($response) || $hasResponse || $hasResponseUserId) {
return; // only send an email if the response is not empty and there is no previous response
}$posturl = $review->assigned_posts($review->ID); //return URL of post
$wbpermalink = ‘<html><Head></Head><Body>Letting you know we have posted a reply to your review at</html>’;//MG
$email = glsr(‘Modules\Email’)->compose([
‘to’ => $review->email,
‘subject’ => ‘[My Website] We have responded to your review!’,
//’message’ => $response,
‘message’ => $wbpermalink,‘headers’ => ‘Content-Type: text/html; charset=”UTF-8″‘, //MG
]);
$email->send();
}, 10, 2);?>
<?php /** * Send an email notification to the review author after responding to a review * This snippet assumes that your review form includes the email field * Paste this code in your theme's functions.php file. * @param \GeminiLabs\SiteReviews\Review $review * @param string $response * @return void */ add_action('site-reviews/review/responded', function ($review, $response) { $hasResponse = !empty($review->response(1)); $hasResponseUserId = !empty(get_post_meta($review->ID, '_response_by', true)); if (empty($response) || $hasResponse || $hasResponseUserId) { return; // only send an email if the response is not empty and there is no previous response } $posturl = $review->assigned_posts($review->ID); //return URL of post $wbpermalink = '<html><Head></Head><Body>Letting you know we have posted a reply to your review at<a href="' .$posturl. '"My Website</Body></html>';//MG $email = glsr('Modules\Email')->compose([ 'to' => $review->email, 'subject' => '[My Website] We have responded to your review!', //'message' => $response, 'message' => $wbpermalink, 'headers' => 'Content-Type: text/html; charset="UTF-8"', //MG ]); $email->send(); }, 10, 2); ?>
Hi – I am asked to enter a password when clicking on the link just provided?
Try again
Thanks so much !!
-
This reply was modified 3 years, 8 months ago by
- The topic ‘Include assigned post in email reply?’ is closed to new replies.