2

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"?

1

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

| improve this answer | |
  • sorry man, no bounty to award anymore. I'm late. – Bikram Mar 19 at 8:45
  • this works in a way, but now every comment is wp_review_comment type. I will try to limit it with conditions. – Bikram Mar 19 at 10:55
  • @Bikram i don't need a bounty. I wanted to give good answer. so you can mark it as accepted if it does what you've described. – Mikhail Mar 19 at 12:37
  • I limited the comment type with -> if ( isset( $_POST['wp_review_comment_rating'] ) ). – Bikram Mar 19 at 13:13
2
+25
    $data = array(
    'comment_post_ID' => $post_id,
    'comment_author' => 'admin',
    'comment_content' => $comment,
    'user_id' => $current_user->ID,
    'comment_date' => $time,
    'comment_approved' => 1,
    'comment_type' => 'custom-comment-class'
);
wp_insert_comment($data);
| improve this answer | |
  • keep some patience, and explain whenever you answer. – Bikram Mar 19 at 8:49
  • Can you please edit your answer and add more details as to how this code resolves the question? – Christine Cooper Mar 19 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.