We recently checked our site in W3C Markup Validation Service and find out that some of our inline javascript in the posts is not properly enclosed in CDATA tags. Read about the details of the issue, reason for it and a bit of hope in future WordPress releases.
Everything inside a CDATA section is ignored by the XML parser and all the inline javascript should be in it. This is how you do it:
<script> <![CDATA<strong>[ </strong>function anything(a,b) { } ]]> </script>
So we fixed all of our plugins which are putting any javascript into the posts and there was another error coming out of the Validation Service saying that the CDATA section is not properly closed.
The generated source code of the page was looking like this:
<script> <![CDATA[ function anything(a,b) { } ]]> </script>
The greater than symbol in CDATA closing tag was replaced by the HTML entity.
We disable both wpautop (this is a function which is automatically inserting paragraphs and newlines into the post – that’s why we hate the default WordPress editor which can’t live without it and use Foliopress WYSIWYG instead) and wptexturize (another nice WordPress function, which used to mess up all of our HTML comments) all the time, so we were very unhappy that there is some other plugin destroying our posts.
After a while I discovered that it’s the WordPress core who is doing this. Let’s take a peek at the structure of the responsible WordPress template tag code:
function the_content() { 1. Get the post content; 2. Apply all the filters on it; 3. Replace ]]> with ]]> 4. Display it; }
So there’s an extra line of code just to mess the CDATA tags. No php comments around it to explain why it’s there. Nothing in the WordPress Codex about it, no sensible answers in support forums.
I had to search the WordPress bug tracking system for an hour to find more about the problem: That single line of code is there for RSS. CDATA closing tag would just break the feeds. That’s is.
Seems like the WordPress guys are aware of this as the issue is planed to be solved for WordPress 2.9 – https://core.trac.wordpress.org/ticket/3670. That means the tag will be converted only in the feeds. Until then our pages are not 100% valid.
However the ticket is 5 years old now with last change 5 weeks ago. I can’t believe WordPress community is fighting this nearly from the beginnings of WordPress.
- Read more about CDATA on w3schools or Wikipedia.
- Read about problems people have with this WordPress issue.
- Take a look at the fix.
Martin Viceník
Martin graduated as an engineer in Computer Science from Slovak Technical University in Bratislava. He grew up in Liptovský Mikuláš in northern Slovakia next to the beautiful Tatra mountains. He is the developer behind our FV Player.
Leave a Reply