How to save new comment as custom comment type?

My previous plugin was saving the comments as type=”wp_review_comment”. I have handled the previous custom fields as below. But I’m having difficulty saving the comments as a custom comment type.

// Add default fields

add_filter('comment_form_default_fields','custom_fields');
function custom_fields($fields) {

        $commenter = wp_get_current_commenter();
        $req = get_option( 'require_name_email' );
        $aria_req = ( $req ? " aria-required='true'" : '' );

        $fields[ 'author' ] = '<p class="comment-form-author">'.
            '<label for="author">' . __( 'Name' ) . '</label>'.
            ( $req ? '<span class="required">*</span>' : '' ).
            '<input id="author" name="author" type="text" value="'. esc_attr( $commenter['comment_author'] ) . 
            '" size="30" tabindex="1"' . $aria_req . ' /></p>';

        $fields[ 'email' ] = '<p class="comment-form-email">'.
            '<label for="email">' . __( 'Email' ) . '</label>'.
            ( $req ? '<span class="required">*</span>' : '' ).
            '<input id="email" name="email" type="text" value="'. esc_attr( $commenter['comment_author_email'] ) . 
            '" size="30"  tabindex="2"' . $aria_req . ' /></p>';

        $fields[ 'url' ] = '<p class="comment-form-url">'.
            '<label for="url">' . __( 'Website' ) . '</label>'.
            '<input id="url" name="url" type="text" value="'. esc_attr( $commenter['comment_author_url'] ) . 
            '" size="30"  tabindex="3" /></p>';

    return $fields;
}

// Add fields.

add_action( 'comment_form_logged_in_after', 'additional_fields' );
add_action( 'comment_form_after_fields', 'additional_fields' );

function additional_fields () {
    echo '<p class="comment-form-title">'.
    '<label for="wp_review_comment_title">' . __( 'Titley' ) . '</label>'.
    '<input id="wp_review_comment_title" name="wp_review_comment_title" type="text" size="30"  tabindex="5" /></p>';

    echo '<p class="comment-form-rating">'.
    '<label for="wp_review_comment_rating">'. __('Ratingy: ') . '<span class="required">*</span></label>
    <span class="commentratingbox">';

    for( $i=1; $i <= 5; $i++ )
    echo '<span class="commentrating"><input type="radio" name="wp_review_comment_rating" id="rating" value="'. $i .'"/>'. $i .'</span>';

    echo'</span></p>';

}

// Save the comment metadata along with the comment.

add_action( 'comment_post', 'save_comment_meta_data' );
function save_comment_meta_data( $comment_id ) {

    if ( ( isset( $_POST['wp_review_comment_title'] ) ) && ( $_POST['wp_review_comment_title'] != '') )
    $title = wp_filter_nohtml_kses($_POST['wp_review_comment_title']);
    add_comment_meta( $comment_id, 'wp_review_comment_title', $title );

    if ( ( isset( $_POST['wp_review_comment_rating'] ) ) && ( $_POST['wp_review_comment_rating'] != '') )
    $rating = wp_filter_nohtml_kses($_POST['wp_review_comment_rating']);
    add_comment_meta( $comment_id, 'wp_review_comment_rating', $rating );
}


// Check if the comment metadata has been filled or not

add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
function verify_comment_meta_data( $commentdata ) {
    if ( ! isset( $_POST['wp_review_comment_rating'] ) )
    wp_die( __( 'A rating is requird. Hit the BACK button and resubmit your review with a rating.' ) );
    return $commentdata;
}

Now, my comments are saved as default comment-type=”comment”. How do I save the comment as a custom comment type “wp_review_comment”?

2 Answers
2

In addition. You already have code which can kind of do what you need.

add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
function verify_comment_meta_data( $commentdata ) {
    if ( ! isset( $_POST['wp_review_comment_rating'] ) )
    wp_die( __( 'A rating is required. Hit the BACK button and resubmit your review with a rating.' ) );
    // this is filter and $commentdata has comment_type parameter in it. so you need to do this
    $commentdata['comment_type'] = 'wp_review_comment';

    return $commentdata;
}

This will do what you wanted but now all your newly created comments will become this type of wp_review_comment, so if you need them the other way – you’ll need some kind of conditionals in this filter instead of forcing all comments to have wp_review_comment_rating and forcing them to be wp_review_comment type or die(), preventing from saving.

See https://core.trac.wordpress.org/browser/tags/5.3/src/wp-includes/comment.php#L1874 for more details

Leave a Comment