Using str_replace to change text within PHP template

I am trying to change some text on my WooCommerce-based WP Website.

Essentially, through a filter using the str_replace I’m trying to change the text ‘%s reviews for %s’ to be something else.

Read More

This text is included in the single-product-reviews.php file.

See below:

<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ($count = $product->get_review_count() ) )
printf( _n( '%s review for %s', '%s reviews for %s', $count,   'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>

In order to do this I’m trying to use the following, but it doesn’t seem to work. I’m not sure what string I should be targetting.

function lnz_replace_content()
{
echo str_replace("%s reviews for %s","%s comments about %s", $product);
}
add_filter('init','lnz_replace_content');'

I’ve had a go with gettext too but that doesn’t seem to work either in this case.

OP Update

I’ve had a go at using gettext, but it doesn’t seem to work in this case.

As previously mentioned, I’m targeting the following code (in single-product-review.php)

<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ( $count = $product->get_review_count() ) )
printf( _n( '%s review for %s', '%s reviews for %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>

If I use ‘gettext’ to replace ‘Reviews’ it works fine. If I try and get it to replace ‘% review for %s’ it doesn’t work.

Any ideas why.

Related posts

2 comments

  1. you can do this with 2 ways:

    1) change in language file:

    msgid "%s reviews for %s"
    msgstr "%s comments about %s"
    
    msgid "%s review for %s"
    msgstr "%s comment about %s"
    

    2) chang code:

    <h2><?php
    if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ($count = $product->get_review_count() ) )
    printf( _n( '%s review for %s', '%s reviews for %s', $count,   'woocommerce' ), $count, get_the_title() );
    else
    _e( 'Reviews', 'woocommerce' );
    ?></h2>
    

    to

    <h2><?php
    if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ($count = $product->get_review_count() ) )
    printf( _n( '%s comment about %s', '%s comment about %s', $count,   'woocommerce' ), $count, get_the_title() );
    else
    _e( 'Reviews', 'woocommerce' );
    ?></h2>
    
  2. You can use the template overrides to override single-product-reviews.php by copying it into your theme’s woocommerce folder.

    or you can filter gettext from your theme’s functions.php

    add_filter( 'gettext', 'theme_change_comment_field_names', 20, 3 );
    /**
     * Change comment form default field names.
     *
     * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
     */
    function theme_change_comment_field_names( $translated_text, $text, $domain ) {
    
        if ( is_singular() ) {
    
            switch ( $translated_text ) {
    
                case '%s reviews for %s' :
    
                    $translated_text = __( '%s comments for %s', 'theme_text_domain' );
                    break;
               case 'Related Products' :
                    $translated_text = __( 'Related Opportunities', 'theme_text_domain' );
                    break;
    
            }
    
        }
    
        return $translated_text;
    }
    

Comments are closed.