• Resolved IstanbulMMV

    (@istanbulmmv)


    I am trying to include a custom comment meta value in a notification email.

    When a visitor leaves a comment on a post they also have the option to add a 1-5 star rating. The star rating is saved using:

    function save_comment_meta_data( $comment_id ) {
    	if ( ( isset( $_POST['rating'] ) ) && ( $_POST['rating'] != '') ) {
    		add_comment_meta( $comment_id, 'rating', absint( $_POST['rating'] ) );
    	}
    }
    add_action( 'comment_post', 'save_comment_meta_data' );

    I am then trying to use this new value in the subject line of the notification email sent to the post author:

    function custom_comment_notification_subject( $subject, $comment_id ) {
            $subject = 'New ' . get_comment_meta( $comment_id, 'rating', true ) . ' star rating received';
    
            return $subject;
    }
    add_filter( 'comment_notification_subject', 'custom_comment_notification_subject', 10, 2 );

    Using the get_comment_meta() call in my email subject line fails with no value returned. The rating value is successfully saved to the database, but it appears wp_mail() executes before the comment meta save action has been completed (hence the empty value). Any suggestions much appreciated.

    • This topic was modified 6 years, 1 month ago by IstanbulMMV.
Viewing 1 replies (of 1 total)
  • Thread Starter IstanbulMMV

    (@istanbulmmv)

    Problem solved! Just add a priority number to the comment_post action…

    add_action( ‘comment_post’, ‘save_comment_meta_data’ ); becomes…
    add_action( ‘comment_post’, ‘save_comment_meta_data’, 5 );

    • This reply was modified 6 years, 1 month ago by IstanbulMMV.
Viewing 1 replies (of 1 total)
  • The topic ‘Using get_comment_meta() in notification emails’ is closed to new replies.