WordPress + CDATA nightmare

Do you know what is worse than not knowing the solution or even the reason to a problem?

Well, that must be, knowing that you have experienced the same problem in the past and resolved it, but no matter how much you try, you can not remember at all what the problem was due and what you did to fix it.

So this evening several hours went by due to a silly and stupid WordPress issue I already fixed once in the past but lost during an upgrade. This time I wanted to make sure I got the solution written down as I would probably forget it again.

On my Marbella news site I was working on embedding an external flash video inside an iframe, which itself was invoked via Javascript to avoid the W3 Validator complaining about XHTML 1.1 and iframes being taboo.

Basically it was WordPress’ over-zealous filters that caught a greater-than sign (>) and converted it to the equivalent HTML entity (>). The most annoying thing is that I already have the Raw HTML plugin installed which disables WordPress’ text filtering. And it also worked fine for the rest of the Javascript and HTML tags in the page text. It was just on one single character, namely in the CDATA closing tag with // ]]> converted to // ]]>.

After searching many different combinations I finally hit it on the nail. I was not alone either.

The annoying culprit is this little line in /wp-includes/post-template.php, which I once more commented out:

$content = str_replace(']]>', ']]>', $content);

For some reason the WordPress developers have not yet fixed this issue after 3 years of it being reported.

As a big fan of open source I appreciate the time and money spent by volunteers in many similar projects, however this is not the first time I see quick to fix issues hanging around for ages, simply due to people preferring to have an academic discussion on who can provide the most elegant solution, instead of simply fixing the issue first and talk later.

Sometimes I wonder how projects ever become real projects.

Anyhow there was a happy ending as you can see for yourself.

Comments

19 responses to “WordPress + CDATA nightmare”

  1. atakan avatar
    atakan

    thank+you+very+very+very+much

  2. Danilo Reis avatar

    Thank+you+very+much,+solved!

  3. Tobi K avatar
    Tobi K

    PS: sorry same problem here 😉 … the second line needs the html entity (‘gt’) in the first parameter, so it works as a special case undo of the first line where it is in second parameter.

    PPS: “the without” is too much

  4. Tobi K avatar
    Tobi K

    regarding the adwords tracking code, instead of killing the line $content = str_replace(‘]]>’, ‘]]>’, $content); you rather get exactly the case you want without minimizing interferences (who knows what the coder really had in mind with this line) by adding this line under: $content = str_replace( ‘/* ]]> */’, ‘/* ]]> */’, $content );

  5. MAI avatar

    TerryLin, for what it is worth I believe the file in question can be found in the file manager within the cpanel of oyur site. I am far from being an expert so don’t quote me on this. Just letting you know what worked for me.

  6. TerryLin avatar

    Hi @Yatin Mulay and @Simon Schönbeck, please help! How can I edit ‘/wp-includes/post-template.php’ and comment out $content = str_replace( ‘]]>’, ‘]]>’, $content ); Thanks!

  7. TerryLin avatar

    Please help me, i am new here.

    I can not find ‘/wp-includes/post-template.php’ and comment out. My Google Adwords tag is missing ‘CDATA’. Please help!

  8. Yatin Mulay avatar
    Yatin Mulay

    Worked like a charm. You’re a Rock Star! Thanks for this solution.

  9. Tharindu avatar
    Tharindu

    +\I+have+commented+out+the+line+,+but+still+Google+tag+manager+shows+it+as+missing+CDATA+.+What+can+be+the+issue.

    here+is+the+code

    /**$content+=+str_replace(+\’]]>\’,+\’]]>\’,+$content+);*/

  10. Serg avatar
    Serg

    Thanks a lot! You save my nerves, I tried before dozen of different plugins!

  11. Simon Schönbeck avatar

    @curra

    That is exactly what I stated in the first three paragraphs 🙂

    You need to do the change each time WordPress is upgraded.

    BR,

    Simon

  12. curra avatar

    Thanks for you help. one question, what happens when you update wordpress?? you have to care of this file is not changed on the update??

  13. Zulu avatar
    Zulu

    Bloody hell Simon YOU ARE A GENIUS!!! It’s unbelievable that this whole nightmare was caused by such a silly mistake and they still refuse to fix it after 3 years!

  14. Simon Schönbeck avatar

    @Norman

    “Comment out” means to make the parser ignore that particular line of code. In PHP which WordPress is written in, you simply add two slashes in front of the particular line of code:

    // line of code goes here

    See http://php.net/manual/en/language.basic-syntax.comments.php for full details.

    BR,

    Simon

  15. Norman avatar

    Hello,

    I think I have this problem in Conversion tracking in AdWords as a I get an error “Missing CDATA Comments”
    I use WordPress as well, and while I can see this exactly within the file you are referring to, I’m not sure what “comment out” means. Does that mean replacing that with what should there, and if so, what exactly should it be?

    Here’s what I have: $content = str_replace( ‘]]>’, ‘]]>’, $content );

    How should that line read?

    I appreciate your assistance if you’re able.

    Thanks,
    Norm

  16. Chen-Pang He avatar

    Why don’t they make it a filter? If it were a filter,

    1. I can remove it or use another filter to make it up.
    2. This makes code more maintainable.

    It is hard-coded. I have no way to make it up other than modifying post-template.php, and I have to remember this after each update.

  17. John avatar
    John

    I’ve run into this problem for the first time. I’m using a plugin which generates javascript with CDATA tags. Altering core is impractical so I’ll have to remove the CDATA, and if that causes validation errors for unencoded ‘>’ characters and what-not, so be it. 🙁

  18. Rich avatar

    Thanks so much for this simon, you’re a genius!

  19. Matt Hawkins avatar

    Three years later and it still isn’t fixed!

Leave a Reply

Your email address will not be published. Required fields are marked *