Search code examples
ajaxwordpresscdatainspectlet

Wordpress class.wp-scripts.php comments out CDATA instead of just removing them


I'm using wp_localize_script to pass some variables from PHP to client side javascript. This function ends up calling another function that does this:

function print_extra_script( $handle, $echo = true ) {
    if ( !$output = $this->get_data( $handle, 'data' ) )
        return;

    if ( !$echo )
        return $output;

    echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
    echo "/* <![CDATA[ */\n";
    echo "$output\n";
    echo "/* ]]> */\n";
    echo "</script>\n";

    return true;
}

Now what is the point of commenting out those CDATA tags, why not just remove them?
This is breaking inspectlet because it is getting screwed up by those extra comments around the CDATA tags.


Solution

  • As far as I guess, because although CDATA is not needed in HTML5, WordPress devs are still including it for blogs and websites, where HTML4 or XHTML is used, because in nearly every case it doesn't do harm.
    I'm looking for a solution myself as I want to get rid of the --in HTML5-- unnecessary type="text/javascript" attribute.

    My best guess so far is, to extend class WP_Scripts from wp-includes/class.wp-scripts.php But I'm still researching on how to best accomplish that.