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.

I want to clean up <script> tags generated by WordPress to produce more semantic output for HTML5.

You can already do this for <style> tags using this code attached to the style_loader_tag filter:

//clean up the default WordPress style tags
add_filter('style_loader_tag', 'clean_style_tag');

function clean_style_tag($input) {
    preg_match_all("!<link rel='stylesheet'\s?(id='[^']+')?\s+href='(.*)' type='text/css' media='(.*)' />!", $input, $matches);

    //only display media if it's print
    $media = $matches[3][0] === 'print' ? ' media="print"' : '';                                                                             
    return '<link rel="stylesheet" href="' . $matches[2][0] . '"' . $media . '>' . "\n";
}

But there isn't an equivalent script_loader_tag in core yet. It was proposed in the past, but for now we need a workaround.

I've started looking in /wp-includes/class.wp-scripts.php at function do_item( $handle, $group = false ) around line 79 which holds the script output (specifically lines 117-120), but I'm having a bit of trouble finding an appropriate filter that could be used here.

share|improve this question
    
What exactly are you trying to do here? From your code, it looks like you want to turn things like <link rel="stylesheet" id="1234" href="http://site.url/style.css" type="text/css" media="screen" /> into <link rel="stylesheet" href="http://site.url/style.css" media="screen" />. So ... remove attributes like "id" and "type". Is this accurate? If so ... why? –  EAMann Apr 27 '12 at 15:22
    
Hi @EAMann - yup, want to clean up the script tags as done for the link tags - so the output would be <script src="source_url"></script>. To answer your question of "why?" it's to produce more semantic output for HTML5 (we don't need those, so they're useless to keep). That WP bug ticket I linked to expresses the same opinion I have about that (which unfortunately isn't in there yet), so looking for a possible workaround. Thanks. –  Zach Apr 27 '12 at 15:46
add comment

1 Answer 1

If you really want to do this, then it should already be possible.

The global $wp_scripts is an instance of the WP_Scripts class, which is an instance of the WP_Dependencies class.

So in theory (not tested), you should be able to do something like this:

function alter_script_tags()
{
    echo '<pre>';
        print $GLOBALS['wp_scripts']->print_html;
    echo '</pre>';
}
add_action( 'wp_enqueue_scripts', 'alter_script_tags', 999999 );

This is just a rough sketch, but you should get the idea.

share|improve this answer
    
It looks like this was attempted a while ago github.com/retlehs/roots/blob/… and I remember testing this and it ran into some issues - I'll continue to research on this one. Thanks. –  Zach Apr 27 '12 at 19:20
    
@Zach Ehm, where do you see print_html over there? The code you linked has nothing to do with what I wrote above. What I´m suggesting is that you intercept the $print_html var which holds the output as this is the only possibility to really alter something instead of replacing it (and therefore staying compatible with Plugins and Themes). Btw: If you appreciate it, then I appreciate receiving an upvote. –  kaiser Apr 28 '12 at 8:47
add comment

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.