VX keeps comment fields on the backend: it stores them, validates them and shows them on the Edit Comment screen. It doesn't add inputs to your site's comment form. When visitors should fill in fields themselves, such as the rating of a review, your theme renders the inputs and hands the submitted values to VX.
That takes four hooks: one to render the inputs, one to set the comment's type, one to check the values before WordPress creates the comment, and one to store them once it exists. The example makes top-level comments on Books into reviews with a rating. The comment type is keyed book_review, and a fieldset keyed review_details has a Comment Type Location that selects it.
php
<?phpuse VxFramework\Comment;use VxFramework\Comment_Type;// Only while VX is active: these hooks call its classes.if ( class_exists( Comment_Type::class ) ) { // 1. The inputs, in the comment form on Books. add_filter( 'comment_form_fields', function ( array $fields ): array { if ( get_post_type() !== 'book' ) { return $fields; } $fields['review_rating'] = '<p class="comment-form-rating">' . '<label for="review-rating">' . esc_html__( 'Rating', 'my-theme' ) . '</label> ' . '<input id="review-rating" name="review[rating]" type="number" min="1" max="5" required>' . '</p>'; return $fields; } ); // 2. The type: decided here from the post, never taken from the form. add_filter( 'preprocess_comment', function ( array $data ): array { $review = Comment_Type::get( 'book_review' ); $post_type = (string) get_post_type( (int) ( $data['comment_post_ID'] ?? 0 ) ); $is_plain = in_array( $data['comment_type'] ?? '', [ '', 'comment' ], true ); if ( $review && $is_plain && empty( $data['comment_parent'] ) && $review->allows_post_type( $post_type ) ) { $data['comment_type'] = 'book_review'; } return $data; } ); // 3. The values, checked before the comment is created. add_filter( 'pre_comment_approved', function ( $approved, array $data ) { if ( is_wp_error( $approved ) || ( $data['comment_type'] ?? '' ) !== 'book_review' ) { return $approved; } $check = Comment::validate_new( 'review_details', my_theme_review_values(), $data ); if ( $check['ok'] && $check['errors'] !== [] ) { return new WP_Error( 'invalid_review', esc_html( $check['errors'][0]['message'] ), 400 ); } return $approved; }, 10, 2 ); // 4. The values, stored once the comment exists. add_action( 'comment_post', function ( int $comment_id, $approved, array $data ): void { if ( ( $data['comment_type'] ?? '' ) !== 'book_review' ) { return; } $comment = Comment::get( $comment_id ); if ( $comment ) { $comment->save_fieldset_values( 'review_details', my_theme_review_values() ); } }, 10, 3 );}function my_theme_review_values(): array { // The comment form carries no nonce for visitors; the submission itself is // what WordPress authorizes, as for the comment's own text. // phpcs:ignore WordPress.Security.NonceVerification.Missing $posted = isset( $_POST['review'] ) && is_array( $_POST['review'] ) ? wp_unslash( $_POST['review'] ) : []; return [ 'rating' => isset( $posted['rating'] ) && $posted['rating'] !== '' ? (int) $posted['rating'] : null, ];}
The form works the same in a classic theme's comments.php and in a block theme's Comments Form block: both render WordPress's comment form, so both run these hooks.
The guard.Comment and Comment_Type are VX's classes. Checking for them before adding the hooks keeps your comment form working if VX is deactivated: visitors can still comment, without the rating. Without the check, step 2 would stop every comment from being posted.
The key.book_review rather than review, which WooCommerce uses for its product reviews. See The key.
The type. WordPress creates every comment from its form as an ordinary comment. Setting the type on the server, from the post and the comment's place in the thread, means nobody can post a review where you didn't ask for one. Check allows_post_type() against the type's Post types, and leave pingbacks, replies and anything a plugin already typed alone.
The check.Comment::validate_new() runs the fieldset's own validation, required fields and ranges included, against the comment about to be created. A WP_Error from pre_comment_approved stops WordPress before anything is saved, and shows the message on its comment error page. ok is false when the fieldset doesn't apply to that comment, such as a reply when the Location only takes top-level comments.
The store.save_fieldset_values() validates again and writes every field of the fieldset. A field you leave out of the values is emptied, which on a new comment is what you want.
The values.$_POST is slashed by WordPress; unslash it. VX sanitizes and validates each value by its field type, so cast only what your inputs need.
The fieldset has to apply to the person submitting. A Location with a Viewer role condition applies only to those roles, so a guest's review would be refused; leave Viewer conditions out of a fieldset visitors fill in. Without one, the Edit Comment screen still keeps the fieldset for moderators, so the author of a Book can't change its reviews' ratings there.
WordPress's REST API creates ordinary comments only: POST /wp/v2/comments refuses any other type. A headless front end that submits reviews needs an endpoint of its own that runs the same three steps and creates the comment with wp_new_comment(). Once a review exists, an app reads its fields from the vx field of /wp/v2/comments, and lists a type with ?type=book_review when the type's Show in REST API setting is on.
Comment forms
VX keeps comment fields on the backend: it stores them, validates them and shows them on the Edit Comment screen. It doesn't add inputs to your site's comment form. When visitors should fill in fields themselves, such as the rating of a review, your theme renders the inputs and hands the submitted values to VX.
That takes four hooks: one to render the inputs, one to set the comment's type, one to check the values before WordPress creates the comment, and one to store them once it exists. The example makes top-level comments on Books into reviews with a rating. The comment type is keyed
book_review, and a fieldset keyedreview_detailshas a Comment Type Location that selects it.The form works the same in a classic theme's
comments.phpand in a block theme's Comments Form block: both render WordPress's comment form, so both run these hooks.What each step does
CommentandComment_Typeare VX's classes. Checking for them before adding the hooks keeps your comment form working if VX is deactivated: visitors can still comment, without the rating. Without the check, step 2 would stop every comment from being posted.book_reviewrather thanreview, which WooCommerce uses for its product reviews. See The key.allows_post_type()against the type's Post types, and leave pingbacks, replies and anything a plugin already typed alone.Comment::validate_new()runs the fieldset's own validation, required fields and ranges included, against the comment about to be created. AWP_Errorfrompre_comment_approvedstops WordPress before anything is saved, and shows the message on its comment error page.okis false when the fieldset doesn't apply to that comment, such as a reply when the Location only takes top-level comments.save_fieldset_values()validates again and writes every field of the fieldset. A field you leave out of the values is emptied, which on a new comment is what you want.$_POSTis slashed by WordPress; unslash it. VX sanitizes and validates each value by its field type, so cast only what your inputs need.Who the fields apply to
The fieldset has to apply to the person submitting. A Location with a Viewer role condition applies only to those roles, so a guest's review would be refused; leave Viewer conditions out of a fieldset visitors fill in. Without one, the Edit Comment screen still keeps the fieldset for moderators, so the author of a Book can't change its reviews' ratings there.
Showing the values
In a
wp_list_comments()callback, read the comment being listed:In a block theme, use
@comment(rating)inside the Comment Template. See Using comment fields.Headless front ends
WordPress's REST API creates ordinary comments only:
POST /wp/v2/commentsrefuses any othertype. A headless front end that submits reviews needs an endpoint of its own that runs the same three steps and creates the comment withwp_new_comment(). Once a review exists, an app reads its fields from thevxfield of/wp/v2/comments, and lists a type with?type=book_reviewwhen the type's Show in REST API setting is on.Related
Technical details for LLMs
wp_new_comment():preprocess_comment→pre_comment_approved(aWP_Errorreturn aborts;wp_handle_comment_submission()passes$avoid_die) → insert →comment_post( $comment_id, $approved, $commentdata ).Comment::validate_new( string $fieldset_key, array $values, array $commentdata )→{ ok: true, errors: [{ path, message }] }or{ ok: false, code, message }(not_found,not_assigned,unsupported_target). Readscomment_post_ID,comment_type(empty →comment),comment_parent,user_id. Viewer conditions are checked for the current user, assave_fieldset_values()checks them.Comment::get( $comment )takes aWP_Commentor id; null for a missing comment or a block-editor note.->save_fieldset_values()and->update_fieldset_values()return{ ok, values?, warnings?, errors? }(Saving values in PHP);->get_fieldset_values(),->field(),->fields(),->has_field(),->field_keys(),get_type(),get_post_id(),get_parent_id(),get_user_id(),get_status().Comment_Type::get( $key )→ null forcommentand unregistered keys;->allows_post_type(),->get_post_types(),->shows_in_rest().