• linzD

    (@linzd)


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

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

    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');'
    
    Your help is much appreciated. 
    
    Thanks in advance.
    
    Linz
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Sorry for being blunt, I mean no offense, I do want to help. You fail to understand the use of filters in so many ways. Don’t worry, we all must start somewhere. In this case a filter is not necessary, but they are an important part of hacking WP, so it’s important you eventually understand if you want to continue doing this. When you have a moment, start here:
    Plugin API

    Now, how to alter the text on your Woo template. You can customize any template in the /templates/ folder structure by placing a copy in your child theme and editing that version. More information here:
    https://docs.woothemes.com/document/template-structure/

    Thread Starter linzD

    (@linzd)

    Hi bcworkz,

    Thanks for that and no worries about being blunt.

    I do know about template overrides via using a Child Theme.

    However, we run a fairly highly customised WordPress/WooCommerce site and I’d like to find ways to do things without changing templates (for the sake of a few words) because it requires a fair amount of re-work for every new iteration of WooCommerce or the plugin.

    I’m enjoying learning how to use snippets of code to do the same and I’ve got add_action and remove_action down to a tee but struggling a little with the filters.

    For the most text, i’ve been using gettext but it doesn’t seem to work when there are variables %s reviews for %s”,”%s comments about %s (like %) involved.

    Are you able to suggest a solution which uses a snippet of code to target specific text? I think it would be super useful because I’d like to change other text throughout the website (some included in plugins) too.

    Thanks for your help.

    Moderator bcworkz

    (@bcworkz)

    OK then, thanks for further explanation, I understand your reluctance to use customized templates, especially for such a small change. Filters it is then.

    Since you’ve successfully used ‘gettext’, the solution may be close at hand – try using ‘ngettext’. But it must be done correctly ?? At first filters seem quite confusing, but for me, at some point I came to a sudden understanding and all was clear and it became rather simple just like that. This will come to you as well. Perhaps a better explanation of hooks than the link I provided above is here:
    https://developer.www.ads-software.com/plugins/hooks/

    I’m unsure why % characters would introduce more trouble. There are escapement issues in some SQL related functions which could be happening here, but I can’t see why. If you still have trouble after this I’ll dig deeper.

    About your filter attempt in your initial post. ‘init’ is not even a filter, it is an action. It serves to allows us to run code at the correct point in time, but there is little we can change about output from actions.

    To use filters, you really need to locate the apply_filters() call in order to properly use a filter. You need to see what is done with the returned value (filters must always return a value), and what parameters are passed to your callback. Your callback must always at least collect the first and return it unchanged if your code does nothing else, for example, if it’s not the string you’re looking for.

    If you collect more than one parameter, the count must be coordinated with the 4th parameter of add_filter(), thus you need to provide the 3rd priority value as well, which defaults to 10. Priorities can be important, they are sort of counter intuitive, the lower number is higher priority and is applied first, but running last is often more desirable because then your code “has the last say”.

    Thread Starter linzD

    (@linzd)

    Hello bcworkz,

    Thanks for your help so far.

    To address your suggestion around ‘ngettext’ logically this seems spot on because the code I am trying to target is within the _n( function.

    I believe this function is essentially to decide (based on $count) whether the text returned is should be $singular or $plural.

    printf( _n( '%s review for %s', '%s reviews for %s', $count, 'woocommerce' )

    I have used this filter successfully.

    `add_filter(‘gettext’, ‘lnz_translate_text’);
    add_filter(‘ngettext’, ‘lnz_translate_text’);

    function lnz_translate_text($translated) {
    $translated = str_ireplace(‘%s reviews for %s’, ‘What do others say?’, $translated);
    $translated = str_ireplace(‘%s review for %s’, ‘What do others say?’, $translated);
    $translated = str_ireplace(‘customer review’, ‘volunteer review’, $translated);
    $translated = str_ireplace(‘customer review’, ‘volunteer reviews’, $translated);
    return $translated;
    }’

    I think the big learning from this is that ‘gettext’ doesn’t work for text within ‘n_(‘ but I’m still not 100% on why or the technical difference between gettext and ngettext.

    Cheers,

    Linz

    Moderator bcworkz

    (@bcworkz)

    They are simply different filters used within different functions. That’s why I like to locate the apply_filters() call in source. Assuming one can make sense of the source, it is clear the filter’s intention and what you can and cannot do with it.

    Confusing the issue is several functions may call a single sub-function, thus a filter in that sub-function will work for many different parent functions. This is why ‘gettext’ is so useful, except not every function uses it. Then we must go to source to find a filter that is used. In this case it happens to be ‘ngettext’.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using str_replace filter in WooCommerce’ is closed to new replies.