Certainly! Below is a complete example of how to:
- Add a Review Title field to the WooCommerce product review form on the frontend.
- Save the review title securely with nonce verification.
- Display the review title along with the review content on the product page.
- Add a Review Title meta box in the WordPress admin to allow editing the review title.
- Save the review title from the admin panel.
Step 1: Add Review Title Field to Review Form (with Nonce)
// Add Review Title field to the WooCommerce review form
add_filter( 'woocommerce_product_review_comment_form_args', 'add_review_title_field_to_review_form' );
function add_review_title_field_to_review_form( $comment_form ) {
$comment_form['fields']['review_title'] = '<p class="comment-form-review-title">
<label for="review_title">' . __( 'Review Title', 'woocommerce' ) . '</label>
<input id="review_title" name="review_title" type="text" value="" size="30" maxlength="100" required />
</p>';
// Add nonce field for security
$comment_form['comment_field'] .= wp_nonce_field( 'save_review_title_nonce', 'save_review_title_nonce_field', true, false );
return $comment_form;
}
Step 2: Save Review Title with Nonce Verification
// Save the review title when a review is submitted
add_action( 'comment_post', 'save_review_title_meta', 20, 2 );
function save_review_title_meta( $comment_id, $comment_approved ) {
// Check if this is a product review
if ( isset( $_POST['comment_post_ID'] ) && 'product' === get_post_type( $_POST['comment_post_ID'] ) ) {
// Verify nonce
if ( ! isset( $_POST['save_review_title_nonce_field'] ) || ! wp_verify_nonce( $_POST['save_review_title_nonce_field'], 'save_review_title_nonce' ) ) {
return;
}
if ( isset( $_POST['review_title'] ) && ! empty( sanitize_text_field( $_POST['review_title'] ) ) ) {
$review_title = sanitize_text_field( $_POST['review_title'] );
add_comment_meta( $comment_id, 'review_title', $review_title, true );
}
}
}
Step 3: Display Review Title on Frontend Review List
// Display the review title above the review content
add_filter( 'woocommerce_review_display_meta', 'display_review_title_in_review', 10, 2 );
function display_review_title_in_review( $comment_text, $comment ) {
$review_title = get_comment_meta( $comment->comment_ID, 'review_title', true );
if ( $review_title ) {
$title_html = '<strong class="woocommerce-review__title">' . esc_html( $review_title ) . '</strong><br>';
return $title_html . $comment_text;
}
return $comment_text;
}
Step 4: Add Review Title Meta Box in Admin Review Edit Screen
// Add meta box to edit review title in admin
add_action( 'add_meta_boxes_comment', 'add_review_title_meta_box' );
function add_review_title_meta_box() {
add_meta_box(
'review_title_meta_box',
__( 'Review Title', 'woocommerce' ),
'render_review_title_meta_box',
'comment',
'normal',
'high'
);
}
function render_review_title_meta_box( $comment ) {
$review_title = get_comment_meta( $comment->comment_ID, 'review_title', true );
?>
<label for="review_title"><?php _e( 'Review Title', 'woocommerce' ); ?></label>
<input type="text" name="review_title" value="<?php echo esc_attr( $review_title ); ?>" class="widefat" />
<?php
}
Step 5: Save Review Title from Admin Meta Box
// Save review title from admin meta box
add_action( 'edit_comment', 'save_review_title_meta_box_data' );
function save_review_title_meta_box_data( $comment_id ) {
if ( isset( $_POST['review_title'] ) ) {
$review_title = sanitize_text_field( $_POST['review_title'] );
update_comment_meta( $comment_id, 'review_title', $review_title );
}
}
Summary
- The review form now includes a Review Title field with nonce security.
- The review title is saved as comment meta.
- The review title is displayed above the review content on the product page.
- Admins can edit the review title in the comment edit screen via a meta box.
How to Use
Add the above code to your theme’s functions.php file or a custom plugin.
If you want me to help you with styling or further customization, just ask!