0

I have a Custom Post Type (CPT) and I want to have a Review system for this post type. I could not find any plugin which support reviews for the posts so I am thinking to do this with my own coding. But I want some guidelines for this so people can leave review for each of the post with their image, name, email and comment.

I am using default comments for my blog posts so I think for these I will need to create a separate comment like system.

4
  • 1
    WordPress Comment system is a kind of a review system. You can use the comment form for that. With some comment_meta fields you can let the user add their additional information (eg. image etc.) or can use any comment attachment plugin for that. So what you need first is to enable comments for your CPT. May 10, 2020 at 18:27
  • @MayeenulIslam thank you for your time. I am using default comments for my blog posts so I think for these I will need to create a separate comment like system. Can we create a custom comment type like we create custom post types.
    – wpdd
    May 10, 2020 at 19:37
  • 1
    Then you are well-advanced. :) You can use custom comment type and can build different form and can store additional information in comment_meta. What's actually stopping you? May 10, 2020 at 21:22
  • @MayeenulIslam I do not know how to create a custom comment type. Is there any function like register_post_type() to create a custom comment type
    – wpdd
    May 11, 2020 at 0:20

1 Answer 1

1

As you are using the default commenting feature for commenting, you can do all on your own. A rough idea on how to do that could be like below:

Step 1: Create Form and Display

Create your own form and enqueue on your post template

// Clear up the alert parameters.
$_SERVER['REQUEST_URI'] = remove_query_arg( 'success', $_SERVER['REQUEST_URI'] );

<form method="post" action="<?php echo esc_url( $_SERVER['REQUEST_URI'] ); ?>">
    <label for="review-content"><?php _e( 'Your Review' ); ?></label>
    <textarea name="review_content" id="review-content" cols="30" rows="4"></textarea>

    <?php wp_nonce_field( 'my-review-nonce' ); ?>

    <button type="submit" name="my_review_form"><?php _e( 'Save' ); ?></button>
</form>

Step 2: Grab Form Data

Intercept the form submission and sanitize input fields

Step 3: Insert Comment with meta data

📕 BIBLE 📕
This WPSE thread is kind of a Bible for now. You might need to hook as gmazzap directed:
Can I store my custom comment_type into wp_comments table?

Use wp_new_comment(), by putting the 'comment_type' to your desired comment type (eg. 'review'). You can use add_comment_meta() for additional information:

<?php
/**
 * Insert Review.
 * 
 * Insert the Review intercepting my review form.
 */
function wpse366293_insert_review() {
    if ( ! isset( $_POST['my_review_form'] ) ) {
        return;
    }

    if ( isset( $_POST['_wpnonce'] ) && ! wp_verify_nonce( $_POST['_wpnonce'], 'my-review-nonce' ) ) {
        return;
    }

    global $current_user;

    // WARNING: Make sure the inputs are properly sanitized.
    $review_id = wp_new_comment(
        array(
            'comment_post_ID'       => absint( $post->ID ), // The post on which the reviews are being recorded.
            'comment_author'        => wp_strip_all_tags( $current_user->display_name ),
            'comment_author_email'  => sanitize_email( $current_user->user_email ),
            'comment_author_url'    => esc_url( $current_user->user_url ),
            'comment_content'       => $response_msg, // Sanitize as per your requirement. You can use wp_kses().
            'comment_type'          => 'review', // Or, your custom comment type.
            'comment_parent'        => 0,
            'user_id'               => absint( $current_user->ID ),
        )
    );

    // If error, return with the error message.
    if ( is_wp_error( $review_id ) ) {
        return $review_id->get_error_message();
    }

    // You can use add_comment_meta() for additional information.
    // add_comment_meta( $review_id, 'my_meta_key', $the_value_i_want );

    // Redirect with a success hint.
    wp_redirect( add_query_arg( 'success', 1, get_the_permalink( $post->ID ) ) );
    exit();
}

add_action( 'template_redirect', 'wpse366293_insert_review' );

Step 4: To display your reviews query them and use loop to display them

You can use the WP_Comment_Query() class or the get_comments() function:

$reviews = get_comments(
    array(
        'post_type'     => 'post', // Could be your CPT.
        'status'        => 'approve',
        'orderby'       => 'comment_date',
        'order'         => 'ASC',
        'type'          => 'review' // Your comment type.
    )
);

if ( $reviews ) :
    foreach ( $reviews as $review ) :
        // Do whatever you want.
        echo wpautop( $review->comment_content );

        // Grab the meta data and display.
        // echo get_comment_meta( $review->comment_ID, 'my_meta_key', true );
    endforeach;
endif;
3
  • Thank you for detailed answer. It is very helpful. Can we add a column in admin panel for comment type(review) and also a comment type(review) in filter ?
    – wpdd
    May 15, 2020 at 3:12
  • Glad it helped. Never tried with the comments column and comment filter. But seeing two hooks on top of the comments list table: restrict_manage_comments, and manage_comments_nav. (Screenshot: prntscr.com/siapz7), so I guess you can add filters. And you can add new columns too. May 17, 2020 at 8:22
  • And, off-note: try to prefix the type string to avoid collision with other plugins and/or future feature incorporation in core. May 17, 2020 at 9:09

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