skip to Main Content

Thank for reading my issue.
Please help that which code snippets can let me change the text "customer reviews" in html below:

<a href="#reviews" class="woocommerce-review-link" rel="nofollow"><span class="count">1</span> customer reviews</a>

I had try this but it not work for me.

add_filter( 'gettext', function( $translated_text ) {
    if ( '**customer reviews**' === $translated_text ) {
        $translated_text = '**reviews**';
    }
    return $translated_text;
} );

Thank you!

2

Answers


  1. This is a little complex so I’ll try to use plain English where possible. If you do a search through the WooCommerce plugin for the string you need, you’ll find this:

    _n( '%s customer review', '%s customer reviews', $review_count, 'woocommerce' )
    

    …which means you can’t use the gettext filter, you need the ngettext one instead as this is a more complex internationalization function:

    add_filter( 'ngettext', 'bbloomer_modify_n_customer_reviews', 9999, 5 );
    
    function bbloomer_modify_n_customer_reviews( $translation, $single, $plural, $number, $domain ) {
        $translation = preg_replace( '%s customer reviews', '%s reviews', $translation );
        return $translation;
    }
    
    Login or Signup to reply.
  2. In WordPress for changing any text showing on front end or on admin you can change with add_filter('gettext', '_translateTextCustom', 100, 3 );

    Try below code snippet

    add_filter('gettext', '_translateTextCustom', 999, 3 );
    function _translateTextCustom( $translated_text, $text, $domain ) {
      global $pagenow, $post_type;
    
      if ( !is_admin() && "customer reviews" === $text && 'woocommerce' === $domain )
      {
        $translated_text =  __( 'reviews', $domain );
      }
      return $translated_text;
    }
    
    1. global $pagenow – You will access all php pages with this global variable.

    in_array( $pagenow, ['post.php', 'post-new.php']

    1. global $post_type – You will access all post types which you want conditionally.

    'product' === $post_type || 'page' === $post_type || 'post' === $post_type

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search