0

I am trying to include a rating comment meta value in the subject line of the email that notifies authors that someone has left a new comment (and, in my case, a rating) on their post.

function mycustomfilter_comment_notification_subject( $subject, $comment_id ) {
        $comment = get_comment( $comment_id );

        $subject = get_the_title( $comment->comment_post_ID ) . ': ' . get_comment_meta( $comment->comment_ID, 'rating', true ) . ' star rating';

        return $subject;
}
add_filter( 'comment_notification_subject', 'mycustomfilter_comment_notification_subject', 10, 2 );

The get_comment_meta() call is failing. It also fails when I try and output the same comment meta data using the comment_notification_text filter.

The rating value is being saved to the database as I can access this variable in a custom wp_list_comments() callback I have prepared.

Any idea why my email code is failing? Many thanks.

2
  • Not sure about an actual answer, but I do note that $comment->comment_ID in get_comment_meta( $comment->comment_ID, 'rating', true ) is unnecessary as you already have the $comment_id variable from the filter so you should use that there instead: get_comment_meta( $comment_id, 'rating', true ). Same in get_the_title() I guess. Might help.
    – Peter HvD
    Aug 21, 2018 at 13:39
  • Thanks Peter. What actually finally fixed the problem was simply adding a priority number of 5 to the comment_post action that was saving my rating value. Aug 22, 2018 at 9:41

1 Answer 1

0

The solution was to add a priority number to the comment_post action that was saving my rating value, as shown below:

function save_comment_meta_data( $comment_id ) {
    if ( ( isset( $_POST['rating'] ) ) && ( $_POST['rating'] != '') ) {
        $rating = wp_filter_nohtml_kses($_POST['rating']);

        add_comment_meta( $comment_id, 'rating', $rating );
    }
}
add_action( 'comment_post', 'save_comment_meta_data', 5 ); // Adding a priority number of 5 fixed the problem!

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.