概要
woocommerce_review_comment_text
フィルタは、WooCommerce の商品のレビューコメントテキストを変更するために使用されます。このフィルタを使用することで、ユーザーが投稿したレビューの内容をカスタマイズすることができ、特定の条件に基づいてテキストを修正したり、追加のHTMLを挿入したりすることが可能となります。
よく使われる機能としては以下のようなものがあります。
1. レビュー中の特定の単語の置き換え
2. HTMLタグの追加や削除
3. スポンサーリンクの挿入
4. 政策や利用規約へのリンク挿入
5. ユーザー評価の表示方法の変更
6. スパムフィルタの実装
構文
add_filter('woocommerce_review_comment_text', 'custom_review_comment_text', 10, 2);
パラメータ
- $comment_text (string): コメントテキスト。
- $comment (WP_Comment): コメントオブジェクト。
戻り値
- (string): フィルタ処理されたコメントテキスト。
使用可能な WooCommerce のバージョン
- 3.0.0 以降
使用可能な WordPress のバージョン
- 4.0 以降
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |
サンプルコード
サンプルコード1
add_filter('woocommerce_review_comment_text', 'replace_banned_words', 10, 1);
function replace_banned_words($comment_text) {
$banned_words = array('悪い', '最低', '偽情報');
return str_replace($banned_words, '***', $comment_text);
}
このサンプルコードは、レビューコメントの中の特定の禁止用語(悪い、最低、偽情報)を***に置き換えます。
サンプルコード2
add_filter('woocommerce_review_comment_text', 'add_custom_html_to_reviews', 10, 2);
function add_custom_html_to_reviews($comment_text, $comment) {
return $comment_text . '<p><strong>このレビューは参考になりますか?</strong></p>';
}
このコードは、レビューの最後に「このレビューは参考になりますか?」というメッセージを表示します。
サンプルコード3
add_filter('woocommerce_review_comment_text', 'highlight_positive_reviews', 10, 1);
function highlight_positive_reviews($comment_text) {
if (strpos($comment_text, '良い') !== false) {
return '<span style="color: green;">' . $comment_text . '</span>';
}
return $comment_text;
}
このサンプルコードは、レビューに「良い」という単語が含まれている場合、そのレビューを緑色で表示します。
サンプルコード4
add_filter('woocommerce_review_comment_text', 'insert_terms_and_conditions_link', 10, 1);
function insert_terms_and_conditions_link($comment_text) {
return $comment_text . ' (注意: 利用規約をお読みください)';
}
このコードは、レビューのテキストの末尾に「利用規約をお読みください」というメッセージを追加します。
サンプルコード5
add_filter('woocommerce_review_comment_text', 'append_credit_message', 10, 2);
function append_credit_message($comment_text, $comment) {
$user_id = $comment->user_id;
$user_info = get_userdata($user_id);
return $comment_text . '<br>- ' . $user_info->display_name;
}
このサンプルコードは、レビューコメントの最後にレビュアーの表示名を追加します。