Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In my plugin, I'm rendering a shortcode with some inline JavaScript. Wordpress seems to hate the closing CDATA tag (]]>), as it escapes it. I'm using CDATA blocks so as to render well-formed XHTML, which allows other processes to scrape the page content easily (legacy systems which suck and are beyond the scope of this question).

Is there a way to get the closing CDATA tag to output as expected?

Steps to Reproduce

Here's a snippet of my code with the unimportant parts removed:

add_shortcode( 'foo', array( $this, 'foo_code' ) );

function foo_code( $a ) { 
  return "
  <script>
  //]]>
  </script>";
}

Expected output

<script>
//]]>
</script>";

Actual output

<script>
//]]&gt;
</script>  

Additional Information

I figured out that the opening <script> tag is triggering the escape. If I change the code to

return "
//]]>
</script>";

It outputs

//]]>

For some reason it doesn't output the closing script tag(!)

It doesn't matter what else is in the JavaScript block: the ]]> tag is always escaped.

I tried html_entity_decode() on the string before returning it, but that didn't do anything.

I've googled and searched the Wordpress.org forums and StackExchange for the solution (or any similar problem) and haven't found anything remotely applicable.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Look at the source of the_content():

function the_content($more_link_text = null, $stripteaser = false) {
    $content = get_the_content($more_link_text, $stripteaser);
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    echo $content;
}

As you can see, there is no filter to prevent that. If you really need inline JavaScript in an XML document you have to escape dangerous characters like < or avoid the_content().

Ruin your day with the six years old Ticket #3670 Removing CDATA close tag ( ]]> ) unbalances the CDATA block. :)

share|improve this answer
1  
+1 on "ruin your day". –  kaiser Oct 12 '12 at 23:15
1  
Wow, that was obscure--thank you. Against my better judgement, I followed that link and read the comments. That sound you heard was my soul trying to claw its way out of my body. –  BryanH Oct 13 '12 at 6:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.