WordPress + CDATA nightmare

9 Apr
2010

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.

19 Responses to WordPress + CDATA nightmare

Avatar

Matt Hawkins

July 31st, 2013 at 17:37

Three years later and it still isn’t fixed!

Avatar

Rich

November 14th, 2013 at 16:35

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

Avatar

John

December 6th, 2013 at 11:29

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. 🙁

Avatar

Chen-Pang He

December 21st, 2013 at 15:43

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.

Avatar

Norman

October 27th, 2014 at 19:19

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

Avatar

Simon Schönbeck

October 28th, 2014 at 10:29

@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

Avatar

Zulu

November 10th, 2014 at 16:25

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!

Avatar

curra

November 17th, 2014 at 16:47

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??

Avatar

Simon Schönbeck

November 17th, 2014 at 19:09

@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

Avatar

Serg

February 13th, 2015 at 15:33

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

Avatar

Tharindu

March 9th, 2015 at 06:00

+\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+);*/

Avatar

Yatin Mulay

June 18th, 2015 at 19:18

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

Avatar

TerryLin

August 19th, 2015 at 10:15

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!

Avatar

TerryLin

August 20th, 2015 at 05:33

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!

Avatar

MAI

September 23rd, 2015 at 01:09

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.

Avatar

Tobi K

October 7th, 2015 at 12:04

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 );

Avatar

Tobi K

October 7th, 2015 at 12:11

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

Avatar

Danilo Reis

November 19th, 2015 at 17:54

Thank+you+very+much,+solved!

Avatar

atakan

December 20th, 2015 at 21:37

thank+you+very+very+very+much

Comment Form

top