• I have tried to use ‘Gettext-filter-Wordpress’ for a eCommerce site I am building, the issue is my client wants two text strings swapped around, I read through the instructions and added the code Which is show below, but I received the following error:

    Fatal error: Cannot redeclare my_text_strings() (previously declared in /homepages/24/d520177130/htdocs/.pomegranate/wp-content/themes/canvas-child/functions.php:70) in /homepages/24/d520177130/htdocs/.pomegranate/wp-content/themes/canvas-child/functions.php on line 91

    The code I used is as follows:-

    /**
    * Change text strings
    *
    * @link http://codex.ww.wp.xz.cn/Plugin_API/Filter_Reference/gettext
    */
    function my_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
    case 'Related Products' :
    $translated_text = __( 'You may also like...', 'woocommerce' );
    break;
    }
    return $translated_text;
    }
    add_filter( 'gettext', 'my_text_strings', 20, 3 );
    /**
    * Change text strings
    *
    * @link http://codex.ww.wp.xz.cn/Plugin_API/Filter_Reference/gettext
    */
    function my_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
    case 'You may also Like...' :
    $translated_text = __( 'Related Products', 'woocommerce' );
    break;
    }
    return $translated_text;
    }
    add_filter( 'gettext', 'my_text_strings', 20, 3 );

    Am I missing something here, and did I need to install a plugin? I am not a programmer so this is a little beyound my normal work area.

Viewing 1 replies (of 1 total)
  • As the error message says, in PHP you can only declare a function (name) once. And you’re doing it twice with the lines

    function my_text_strings(...

    You should be better off using something like this

    <?php
    /**
    * Change text strings
    *
    * @link http://codex.ww.wp.xz.cn/Plugin_API/Filter_Reference/gettext
    */
    function my_text_strings( $translated_text, $text, $domain ) {
    
      switch ( $translated_text ) {
    
        case 'Related Products' :
          $translated_text = __( 'You may also like...', 'woocommerce' );
          break;
    
        case 'You may also Like...' :
          $translated_text = __( 'Related Products', 'woocommerce' );
          break;
    
        // add more cases if you like
    
        }
    
      }
    
      return $translated_text;
    
    }
    add_filter( 'gettext', 'my_text_strings', 20, 3 );
    ?>

    instead of the code block you’ve posted.

    And instead of my_text_strings you should come up with a more unique and descriptive name.

Viewing 1 replies (of 1 total)

The topic ‘Gettext Filter issue’ is closed to new replies.