I want to add a custom field to my reviews form on WooCommerce just like this image:
![enter image description here](https://i.stack.imgur.com/khGXT.png)
And then how to get the output of that title just like that:
![enter image description here](https://i.stack.imgur.com/5LIDy.png)
我只知道如何创建一个新字段单一产品评论.php文件中添加该代码:
$comment_form['comment_field'] .= '<p class="comment-form-title"><label for="title">' . esc_html__( 'Review title', 'woocommerce' ) . ' <span class="required">*</span></label><input id="title" name="title" type="text" aria-required="true" required></input></p>';
但是,如何将其保存在数据库中以及如何在评论内容上方输出此标题?
EDIT:我尝试了很多方法,直到通过在我的子主题的functions.php 上编写此代码来实现我想要的一些目标。
1) 在评论评论表单中添加自定义字段“评论标题”:
function add_review_title_field_on_comment_form() {
echo '<p class="comment-form-title uk-margin-top"><label for="title">' . __( 'Review title', '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' );
2)将该字段值保存在wp_commentmeta数据库中的表:
add_action( 'comment_post', 'instacraftcbd_review_title_save_comment' );
function instacraftcbd_review_title_save_comment( $comment_id ){
if( isset( $_POST['title'] ) )
update_comment_meta( $comment_id, 'title', esc_attr( $_POST['title'] ) );
}
3) 使用以下命令检索该字段输出值:
var $title = get_comment_meta( $comment->comment_ID, "title", true );
echo $title;
现在唯一缺少的事情是,如何将该字段的输出放置在评论文本或评论文本之前?