How can I modify what is being output in wp_head, whether by a theme or WordPress in general?

I’ve been working on performance of my site using YSLOW, and noticed that WordPress it outputting things in my document head that I know I don’t need.

For example, I’m already calling jQuery 1.9 from the Google CDN, so I don’t need the call to jQuery in wp-includes.

Read More

Also, a plugin is including a stylesheet which I don’t need (because I’m overwriting 99% of those styles anyway in my theme), so I’d like to get rid of that call also.

So how do I edit what is being output by the wp_head() function, whether WordPress is putting it there (like the wp-includes jQuery call) or whether a plugin is putting it there (like the stylesheet call)?

Related posts

Leave a Reply

2 comments

  1. First: don’t enqueue custom versions of WordPress core-bundled scripts, including (and especially) jQuery.

    Second, to answer your question: those Plugin scripts and stylesheets are enqueued, using add_action(), via a callback hooked into one of the following action hooks:

    • wp_head
    • wp_enqueue_scripts
    • wp_print_scripts
    • wp_print_styles

    (There are others, but those are the most likely.)

    Inside the callback, the following functions are used to enqueue:

    So, for a Plugin-enqueued stylesheet, named foobar.css, you’d need to look in the Plugin files for calls to wp_enqueue_style(), then note the name of the callback function it is called within. Then, find the add_action() call that references that callback function. e.g.:

    add_action( 'wp_head', 'pluginname_enqueue_styles' );
    

    Once you’ve found that call, you can override it yourself, using remove_action():

    remove_action( 'wp_head', 'pluginname_enqueue_styles' );
    
  2. I don’t agree that overriding default versions with a CDN-hosted version is a bad practice. The onus is on you, however, to ensure you’re using a version which all of your plugins are compatible with. Be sure to test thoroughly.

    To override the default jQuery with the CDN version, add something like this to your functions.php :

    function replace_jquery() {
        if (!is_admin()) {
            wp_deregister_script('jquery');
            wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', false, '1.9.1');
            wp_enqueue_script('jquery');
        }
    }
    add_action('init', 'replace_jquery');