• I recently upgraded to 4.1 from 3.something and a filter I had set up to alter some text and replace it with some html, now displays the raw html characters.

    I do the following:

    add_filter( 'gettext', "my_func", 20, 3 );
    
    function my_func($translated_text, $text, $domain ) {
      if ($translated_text == "target text") {
        $translated_text = __("target<br />text");
      }
      return $translated_text
    }

    … and what I see is exactly this:

    target<br />text

    instead of this

    target
    text

    When I examine the html, I can see that the angle brackets have been replaced with their

    & lt; and & gt;

    equivalents.

    So what do I have to do if I want to replace text with some HTML?

Viewing 4 replies - 1 through 4 (of 4 total)
  • @batman42ca, try this…

    add_filter( 'gettext', "my_func", 20, 3 );
    
    function my_func($translated_text, $text, $domain ) {
      $new_content = '';
      if ($translated_text == "target text") {
        $translated_text = __("target<br />text");
      }
      foreach( preg_split( '/((\r?\n)|(\r\n?))/', $translated_text ) as $line ) {
        $new_content .= preg_replace( '/^>/', '>', $line ) . '\r\n';
      }
      return $new_content;
    }
    Thread Starter batman42ca

    (@batman42ca)

    Thanks but that won’t work. The $translated text in my sample is already the correct HTML. It’s whatever happens after it is returned by my function that is the problem. My problem is that I want the

    <br />

    to remain that way and be echoed to the browser as an HTML line break. Instead what I see displayed is a literal less than sign, the text “br /” and then the greater than sign even though I’m returning the proper HTML from my function.

    I checked my return value before I returned it and it was correct. Something somewhere else is changing it.

    @batman42ca, Then you don’t need to amend the above function.

    Try to add this function (filter)…

    function dcg_html_fix( $content ) {
        $new_content = '';
        foreach( preg_split( '/((\r?\n)|(\r\n?))/', $content ) as $line ) {
            $new_content .= preg_replace( '/^>/', '>', $line ) . '\r\n';
        }
        return $new_content;
    }
    add_filter( 'the_content', 'dcg_html_fix', 1 );

    Alternatively, you can simply disable/remove tags using below function.

    function dcg_prevent_tags(){
        remove_filter('the_content', 'wpautop');
    }
    add_action('init', 'dcg_prevent_tags');

    Let me know how it goes!

    Thread Starter batman42ca

    (@batman42ca)

    I appreciate the help, but I don’t think you’re fixing the problem I’m having. This has nothing at all to do with /r or /n characters. This has everything to do with HTML angle brackets being encoded.

    The angle brackets are being turned into HTML entities. See this page:

    https://www.w3schools.com/html/html_entities.asp

    I need WordPress to stop turning my angle brackets into HTML entities. This seems to be something new since I upgraded to WordPress 4.1

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘gettext filter alters html’ is closed to new replies.