Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

Overwrite action attribute of WooCommerce Product Review form

Product Reviews are an effective way to help your customers make a decision on the purchase. At Tyche Softwares, we always ask our customers to review our plugins & support on any one of our social media channels. Apart from helping customers make a decision, these reviews also are very encouraging for our team members.

At times, you might want to do some changes in the way the product reviews submitted for WooCommerce products are handled. The changes could be as simple as adding some text to the form or adding a new field or filtering the data that is submitted.

The Product Review form uses the wp-comments-post.php file for adding comments. This is the same file that is used by WordPress for comments from any Posts & Pages as well. If you want to add the reviews in database via some other file, then you will need to change the “action” attribute of the Product Review form.

WooCommerce Product Review form
WooCommerce Product Review form

WordPress provides a filter comment_form_defaults that allows you to change the “action” attribute along with many other parameters. However, if you use that filter to change the “action” attribute, then it will change all the comment forms including that on Posts & Pages.

Instead, we can use another filter provided by WooCommerce woocommerce_product_review_comment_form_args, which only changes the “action” attribute on the Product Review form & not elsewhere. You can add the following code in your plugin or in your child theme’s functions.php file.

add_filter( 'woocommerce_product_review_comment_form_args', 'overwrite_submit_url_in_product_review_form', 10, 1 );

function overwrite_submit_url_in_product_review_form( $defaults ) {
    $defaults[ 'action' ] = plugins_url() . '/modify-comments-form/comments-post.php';
    return $defaults;
}

In the above example, I have simply pointed the form to a file named comments-post.php within my plugin. The plugin only has a few lines of code:

<?php
/*
Plugin Name: Overwrite Product Review Comment form
Plugin URI: https://www.tychesoftwares.com/
Description: A simple plugin that allows to overwrite the WooCommerce Product Review form in different ways
Version: 1.0
Author: Vishal Kothari
Author URI: https://profiles.wordpress.org/ashokrane
*/

add_filter( 'woocommerce_product_review_comment_form_args', 'overwrite_submit_url_in_product_review_form', 10, 1 );

function overwrite_submit_url_in_product_review_form( $defaults ) {
    $defaults[ 'action' ] = plugins_url() . '/modify-comments-form/comments-post.php';
    return $defaults;
}

Adding the above code will submit your Product Review form fields to the comments-post.php file in your plugin & there you can do any changes as you desire. You might want to use a different function instead of the wp_handle_comment_submission() used by WordPress to insert comments in database.

There are several cases when you might want to do that like adding more fields over the default ones, or skipping the validation checks for certain fields.

Browse more in: WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x