How to Send Notifications to the User Whose Post Received a Comment

Comment Notification Email Send to Editor User role

I found answer!

I Created New user with Editor user Role.To send your notifications to a particular to New created Editor User I added Editor User Id "5" following code.

function se_comment_moderation_recipients( $emails, $comment_id ) {
$comment = get_comment( $comment_id );
$post = get_post( $comment->comment_post_ID );
$user = get_user_by( 'id', '5' );

// Return only the post author if the author can modify.
if ( user_can( $user->ID, 'edit_published_posts' ) && ! empty( $user->user_email ) ) {
$emails = array( $user->user_email );
}

return $emails;
}
add_filter( 'comment_moderation_recipients', 'se_comment_moderation_recipients', 11, 2 );
add_filter( 'comment_notification_recipients', 'se_comment_moderation_recipients', 11, 2 );

Reference

And this works!

How to send comment notification message to website admin and post Author using functions.php?

I am using Multiple user comment notification, this below code is almost covered my requirement.

add_filter('comment_notification_recipients', 'override_comment_notice_repicient', 10, 2);
function override_comment_notice_repicient($emails, $comment_id) {
$admins = get_users( array(
'role__in' => array('administrator'),
) );
foreach ( $admins as $user ) {
$emails[] = $user->user_email;
}
return ($emails);
}

Above code is from this link

How to show comments notification in php

The way I would approach this is to use a client side AJAX request to a php fetch script that looks at the logged in users SESSION['id'] to find all comments made by post authors that are children of a comment the logged in user has made

Frontend

You may use whatever way you want to send a request to your PHP script, I have used jQuery for simplicity.

The jQuery $.ajax function will be ran on the page continuously to check for notifications. This has been done by enclosing it in a setInterval function.

This code is not tested so not 100% sure on the namespacing of the PHP array you will get back. Best to console.log the data object in the success handler here.

You will notice that there is reference to a readComment.php script, this can be a simple script that sets the comments.status = 1 to indicate read status.


setInterval(function() {
$.ajax({
url:"fetch.php",
method:"GET",
success:function(data)
{
console.log(data)
data.forEach(function(i,v) {
$( "#notifications" ).append( "<a href=readComment.php?id=" + v.id + ">unread notification</p>" );
});

}
});
}
}, 60 * 1000); // This will run every minute to check for notifications

Server

fetch.php

I am using a select statement here with two WHERE IN clauses to retrieve all comments made by post authors that are children of a comment the logged in user has made. I am not 100% on this query either as I have not tested but this should get you started.

$query = "SELECT 
*
FROM
comments
WHERE
comments.user_id IN (SELECT posts.post_user FROM posts)
AND
comments.parent_comment_id IN (SELECT comments.id FROM comments WHERE comments.user_id = ".SESSION['id'].")"
AND
comments.status = 0";

$result = mysqli_query($connection, $query);

header("Status: 200");
header('Content-Type: application/json');
echo json_encode(array('data' => mysqli_fetch_array($result) ));
die;

Let me know if you need more information.

How would you design email notifications for a threaded comments system?

Ended up going with 2 settings that are changeable by the user:

Email me for any new root level comments       [X] On   [] Off
Email me for any new comment replies [X] On [] Off


Related Topics



Leave a reply



Submit