如何添加“评论标题"WooCommerce 评论表上的字段? [英] How to add a "Review title" field on WooCommerce reviews form?

查看:25
本文介绍了如何添加“评论标题"WooCommerce 评论表上的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 WooCommerce 上的评论表单中添加一个自定义字段,如下图所示:

I want to add a custom field to my reviews form on WooCommerce just like this image:

然后如何像这样获得该标题的输出:

And then how to get the output of that title just like that:

我只知道如何通过添加该代码在 single-product-reviews.php 文件上创建一个新字段:

I just know how to create a new field on the single-product-reviews.php file by adding that code:

$comment_form['comment_field'] .= '<p class="comment-form-title"><label for="title">' . esc_html__( 'Review title', 'woocommerce' ) . '&nbsp;<span class="required">*</span></label><input id="title" name="title" type="text" aria-required="true" required></input></p>';

但是,如何将其保存在数据库中以及如何在评论内容上方输出此标题?

But, how can I save this on the database and how can I output this title above comment content?

我已经尝试了很多方法,直到通过在我的子主题的functions.php 上编写此代码来实现我想要的一些东西.

I have tried many ways until I achieve some of what I want by writing this code on functions.php on my child theme.

1) 在评论评论表单上添加自定义字段评论标题":

1) Adding the custom field "Review title" on reviews comment form:

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 表中:

2) Save that field value on wp_commentmeta table on the database:

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) 使用以下方法检索该字段的输出值:

3) Retrieve that field output value by using this:

var $title = get_comment_meta( $comment->comment_ID, "title", true );
echo $title;

现在唯一缺少的东西是,如何将该字段的输出放在评论文本或评论文本之前?

Now the only missing thing, how can I place the output of that field just before the comment text or review text?

推荐答案

自己找到解决方案太好了,这是我正在寻找的答案,也许可以帮助您!

It's too good to find a solution myself, this my answer of what I'm looking for, maybe can help you!

1) 转到父主题或子主题上的 functions.php,然后粘贴下面的代码以在评论评论表单上添加自定义字段评论标题":

1) Go to your functions.php on your parent or child theme then paste that code below to add the custom field "Review title" on reviews comment form:

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 表中:

2) Save that field value on wp_commentmeta table on the database by adding this code just above our last code:

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'] ) );
}

3) 如果您想检索该字段输出值,请使用以下代码:

3) If you want to retrieve that field output value use that code below:

var $title = get_comment_meta( $comment->comment_ID, "title", true );
echo $title;

注意:它仅适用于评论循环!

4) 要在每个评论文本之前添加该字段输出,您必须在 functions.php 上创建一个新函数,如下所示:

4) To add that field output before each comment text, you have to create a new function on functions.php just like this:

function get_review_title( $id ) {
    $val = get_comment_meta( $id, "title", true );
    $title = $val ? '<strong class="review-title">' . $val . '</strong>' : '';
    return $title;
}

然后确保将此代码添加到 WooCommerce 模板文件 review.php 或者您可以使用 woocommerce_review_before_comment_meta 钩子,但在我的情况下,我已经写了那个代码:echo get_review_title( $comment->comment_ID );

And then be sure to add this code below to that WooCommerce template file review.php or you can use woocommerce_review_before_comment_meta hook, but in my case, I've written that code: echo get_review_title( $comment->comment_ID );

刚刚结束

do_action('woocommerce_review_before_comment_meta', $comment);

希望能帮到你!

这篇关于如何添加“评论标题"WooCommerce 评论表上的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆