skip to Main Content

I’m currently working on a WooCommerce theme which I have written a custom script to show the reviews rather than the default template.

The issue is I need to add the form separately to the theme which really needs to come from comments_template() so it will work correctly permanently with updates.

echo comments_template(); // Loads comments and form - I would like only the form

As some products can have hundreds or thousands of reviews I don’t want to simply hide them using CSS, I would prefer to use comments_template() just to get the form and not load in the comments as well.

Does anyone have any idea how to achieve this with a filter or other function?

2

Answers


  1. Chosen as BEST ANSWER

    As it doesn't look like there is a good solution for this here is the workaround I used.

    In the Wordpress discussion settings I just changed the "Break comments into pages with 1 top level comments per page and the last page displayed by default" setting to 1 so it only shows 1 comment then hide the element via CSS.

    Wordpress Discussion Settings

    This only works if you have custom code to show the comments, if you rely on the above for other comment sections as well it won't be a viable solution.


  2. i dont no if this will help you but ive made a function so i can put the form anywhere on the site with a shortcode. this is the code

    function custom_review_form_shortcode($atts) {
        ob_start();
        ?>
    
    
        <form id="custom-review-form" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="post">
            <input type="hidden" name="action" value="custom_review_form_submission">
            <input type="hidden" name="custom_review_form_nonce" value="<?php echo wp_create_nonce('custom_review_form_nonce'); ?>" />
    
            <label for="product-select">Selecteer een product</label>
            <select name="product_id" id="product-select">
                <?php
                $products = get_posts(array('post_type' => 'product', 'posts_per_page' => -1));
                foreach ($products as $product) {
                    echo '<option value="' . $product->ID . '">' . $product->post_title . '</option>';
                }
                ?>
            </select>
    
            <label for="name">Naam</label>
            <input type="text" name="name" id="name" required>
    
            <label for="email">Email</label>
            <input type="email" name="email" id="email" required>
    
            <label for="rating">Beoordeling</label>
            <select name="rating" id="rating" required>
                <option value="5">5 Sterren</option>
                <option value="4">4 Sterren</option>
                <option value="3">3 Sterren</option>
                <option value="2">2 Sterren</option>
                <option value="1">1 Ster</option>
            </select>
    
            <label for="comment">Uw Review</label>
            <textarea name="comment" id="comment" required></textarea>
    
            <?php
            // Include the WooCommerce review form
            comments_template('woocommerce/single-product-reviews.php');
            ?>
    
            <input type="submit" value="Submit Review">
        </form>
        <?php
        return ob_get_clean();
    }
    
    add_shortcode('custom_review_form', 'custom_review_form_shortcode');
    
    // Form submission handling
    add_action('admin_post_nopriv_custom_review_form_submission', 'handle_custom_review_form_submission');
    add_action('admin_post_custom_review_form_submission', 'handle_custom_review_form_submission');
    
    function handle_custom_review_form_submission() {
        // Verify nonce
        if (isset($_POST['custom_review_form_nonce']) && wp_verify_nonce($_POST['custom_review_form_nonce'], 'custom_review_form_nonce')) {
            // Get form data
            $product_id = $_POST['product_id'];
            $name = sanitize_text_field($_POST['name']);
            $email = sanitize_email($_POST['email']);
            $rating = intval($_POST['rating']);
            $comment = sanitize_text_field($_POST['comment']);
    
            // Prepare review data
            $review_data = array(
                'comment_post_ID' => $product_id,
                'comment_author' => $name,
                'comment_author_email' => $email,
                'comment_content' => $comment,
                'user_id' => 0, // Guest user
                'comment_approved' => 1, // Auto-approve the comment
            );
    
            // Insert the review into the database
            $comment_id = wp_insert_comment($review_data);
    
            // Set the rating for the review
            add_comment_meta($comment_id, 'rating', $rating);
    
            // Redirect users after form submission (change the URL as needed)
            wp_redirect(home_url('/home')); // Redirect to a thank you page
            exit;
        } else {
            // Nonce verification failed, handle the error appropriately (e.g., redirect to an error page)
            wp_redirect(home_url('/error')); // Redirect to an error page
            exit;
        }
    }
    

    This code will also give the form a dropdown menu so the costumer can choose about what product they want to write a review about (the labels are in dutch so you need to change this to your own langue).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search