Reputation: 2192
Here is the code I am using to add the title on woocommerce review form :
function add_review_title_field_on_comment_form() {
echo '<p class="comment-form-title uk-margin-top"><label for="title">' . __( 'Review Headline', 'text-domain' ) . '</label><input class="uk-input uk-width-large uk-display-block" type="text" name="title" id="title"/></p>';
}
add_action( 'comment_form_logged_in_after', 'add_review_title_field_on_comment_form' );
add_action( 'comment_form_after_fields', 'add_review_title_field_on_comment_form' );
add_action( 'comment_post', 'save_comment_review_title_field' );
function save_comment_review_title_field( $comment_id ){
if( isset( $_POST['title'] ) )
update_comment_meta( $comment_id, 'title', esc_attr( $_POST['title'] ) );
}
function get_review_title( $id ) {
$val = get_comment_meta( $id, "title", true );
$title = $val ? '<strong class="review-title" style="font-size:16px;">' . $val . '</strong>' : '';
return $title;
}
Due to this code, I can able to add headline filed for review form but it's not required please check
Can anyone suggest me how to make this field required please check the screenshot, I know what to do to make any field required but it's coming as a functions to functions.php that's the problem.
Thanks
Upvotes: 1
Views: 749
Reputation: 2011
Html5 has default required
validation so add, check below code for this.
function add_review_title_field_on_comment_form() {
echo '<p class="comment-form-title uk-margin-top"><label for="title">' . __( 'Review Headline', 'text-domain' ) . '</label><input class="uk-input uk-width-large uk-display-block" type="text" name="title" id="title" required/></p>';
}
And for server side add code in your function , for that check below code.
function save_comment_review_title_field( $comment_id ){
if( isset( $_POST['title'] ) && ! empty($_POST[ 'title' ]))
update_comment_meta( $comment_id, 'title', esc_attr( $_POST['title'] ) );
else
wp_die( __('Please enter a valid title, its required.') );
}
Hope this will help you.
Upvotes: 1