Example of using CDATA with wp_add_inline_script

When using wp_add_inline_script, a CDATA section is required if you need your document to parse as XML (e.g. when an XHTML page is interpreted as XML) and you want to write literal i<10 and a && b instead of i<10 and a && b.

This is not an issue with scripts that are stored in external source files, but for any inline JavaScript in XHTML you will probably want to use a CDATA section. Note that many XHTML pages were never intended to be parsed as XML in which case this will not be an issue.

<?php
wp_add_inline_script(
'handle',
'// <![CDATA[
// Custom JS here.
// ]]>'
);
view raw functions.php hosted with ❤ by GitHub

Reader Interactions

Leave a Reply

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