Falcon1986-Online

Home of a med student who likes to manage websites and talk tech!

Remove unwanted WordPress header elements

with 206 comments

WordPress LogoI was performing a bit of optimization on one of my WordPress blogs and wanted to clean up the header. The default installation of WordPress adds quite a bit up there and this bloat can be compounded by a theme’s own customizations. After quite a bit of searching around Google, I came up with a clean solution. Here is what I did to help de-clutter my theme’s header.

In addition to custom Javascript and CSS calls that may clutter your theme’s header.php file, you may find some other elements whose features you will never use or could do without. However, these elements usually only appear when viewing your pages through your browser, because activated plugins will automatically insert them via PHP. Take, for example, WP-PageNavi. This is a great plugin for placing numbered page links in your WordPress blog, but it inserts a call to its own stylesheet. Being the optimization junkie that I am, it would be better for me to just combine WP-PageNavi’s stylesheet with my theme’s stylesheet so a single CSS file loads all of the styles. The problem is getting rid of WP-PageNavi’s call to its own stylesheet without deactivating the plugin altogether.

Many of these header insertions are being called by the wp_head() hook. Look in your theme’s header.php file and you will see it there. If you think that simply removing the hook will eliminate these extra insertions, you are correct, but it doesn’t come without consequences. Doing so may completely cripple the functionality of your blog because there are other necessary insertions that are required to make your theme work properly. You can, however, disable individual insertions without removing the wp_head() hook, which gives you more control.

In order to achieve removal of plugins hooking into wp_head() you will need to look at that plugin’s main PHP file. Look for functions that look like this:

add_action('wp_head', 'plugin_function_here');

Adding the following line to your theme’s functions.php file and saving it will disable that insertion via wp_head():

remove_action('wp_head', 'plugin_function_here');

Reload a page in your web browser and view the source to see the changes. Quite self-explanatory and simple, isn’t it? Just be careful of what you remove.

There are unique cases where forcing a remove_action from wp_head() does not work. In the case of WP-PageNavi, there is no explicit action being added to wp_head(), but it will still load its own stylesheet. If you look at wp-pagenavi.php you will notice the ‘Enqueue PageNavi Stylesheets’ function that inserts the stylesheet via a wp_print_styles action:

add_action('wp_print_styles', 'pagenavi_stylesheets');

To stop WP-PageNavi’s stylesheet from being called in the header, all you need to do is add the following to your theme’s functions.php file:

remove_action('wp_print_styles', 'pagenavi_stylesheets');

By now you should be noticing a trend. Find the function, then remove the function. Here are some other elements that you can remove that are in direct reference to wp_head(). Note that you should only disable these features if you do not use them.

// Really Simple Discovery
remove_action('wp_head', 'rsd_link');
// Windows Live Writer
remove_action('wp_head', 'wlwmanifest_link');

You can take things a bit further as I have done. I noticed that my theme calls for jQuery Javascript through wp_head() plus a few other Javascript files outside of wp_head(). Again, I was looking at a way of possibly combining and compressing my theme’s Javascript, but how would I disable jQuery loading through wp_head()? I could not find a function reference that would allow me to remove it and trying remove_action('wp_head', 'jquery'); didn’t work. I did find that wp_deregister_script('jquery'); could work perfectly. However, if placed in your theme’s functions.php file, the WordPress Dashboard admin area would become seriously crippled! No longer would the fancy drop-down menus work and various other features stopped working properly all of a sudden. Obviously, the admin area requires jQuery (duh!), so I had to remove the entry from my theme’s functions.php file. Being inquisitive as I am, I placed wp_deregister_script('jquery'); in my theme’s header.php file instead and, to my surprise, doing this disabled jQuery from loading in the theme yet allowed the admin area to retain full functionality.

wp-sshot

Be aware that my theme still utilizes jQuery. It is just that I have chosen to pack all of my Javascript together and call for it through a static URL within script tags.

I hope this post is helpful to persons who are interested in gaining more control over their theme’s header and reducing all of the clutter. Please be careful of what you edit and, of course, have fun with WordPress!

About these ads

Written by falcon1986

15 July, 2009 at 10:25 AM

206 Responses

Subscribe to comments with RSS.

  1. What i do not realize is in reality how you’re now
    not really much more smartly-favored than you may be right now.
    You’re very intelligent. You realize thus considerably when it comes to this topic,
    produced me individually consider it from numerous varied
    angles. Its like women and men don’t seem to be
    involved unless it is something to accomplish
    with Girl gaga! Your own stuffs nice. All the time maintain it up!

  2. Link exchange is nothing else but it is only placing the other person’s webpage link on your page at proper place and other person will also do similar in favor of you.

  3. After I originally commented I seem to have clicked on the -Notify me when new comments are
    added- checkbox and now every time a comment is added I get four
    emails with the same comment. Is there a way you are able to remove me from
    that service? Many thanks!

  4. Hi to every , as I am really keen of reading this webpage’s post to be updated on a regular basis.
    It contains pleasant data.

    remodel house exterior

    25 August, 2014 at 12:55 PM

  5. Hey there this is kinda of off topic but I was wanting to know if
    blogs use WYSIWYG editors or if you have to
    manually code with HTML. I’m starting a blog soon but have no coding skills so I wanted to get guidance from someone with experience.

    Any help would be enormously appreciated!

    poulan riding mower

    26 August, 2014 at 1:22 AM

  6. Why people still make use of to read news papers when in this
    technological world all is presented on net?

    cash bail bond

    26 August, 2014 at 2:56 AM

  7. I have learn some just right stuff here. Certainly worth bookmarking for revisiting.
    I wonder how much effort you set to make this type of magnificent informative web site.

  8. I don’t even know how I finished up here, however I assumed
    this put up was good. I do not realize who you might be however definitely you’re going to
    a well-known blogger if you happen to are not already.
    Cheers!

  9. My brother recommended I might like this web site. He was totally right.
    This post truly made my day. You can not imagine simply how much
    time I had spent for this info! Thanks!

  10. We are a bunch of volunteers and opening a new scheme in our community.
    Your web site provided us with valuable info to work on. You have done an impressive activity and our entire group will probably be grateful to you.

  11. It’s wonderful that you are getting thoughts from this article as well as from our discussion made here.

    Auto Repair Sacramento

    29 August, 2014 at 3:20 PM

  12. Hello I am so happy I found your web site, I really found you by error, while I was
    researching on Digg for something else, Nonetheless I am here now and would just like
    to say many thanks for a incredible post and a all round enjoyable blog
    (I also love the theme/design), I don’t have time to
    look over it all at the moment but I have book-marked it and also included your RSS feeds,
    so when I have time I will be back to read much more, Please do keep up the great b.

    bondsman hartford ct

    30 August, 2014 at 4:28 AM

  13. Hi to all, how is the whole thing, I think every one is getting
    more from this site, and your views are
    pleasant designed for new users.

    lake hartwell boat rentals

    31 August, 2014 at 5:53 AM

  14. Hi there to all, how is the whole thing, I think every one is
    getting more from this website, and your views are fastidious for new
    viewers.

    get free images

    31 August, 2014 at 8:22 AM

  15. hello there and thank you for your information – I have definitely picked up anything new from right here.
    I did however expertise some technical issues using this web site,
    since I experienced to reload the site many times previous to I could get it to load correctly.
    I had been wondering if your web hosting is OK? Not that I am complaining, but slow loading instances times will very frequently affect your
    placement in google and could damage your quality score if ads and marketing with Adwords.
    Anyway I am adding this RSS to my email and could look
    out for much more of your respective intriguing content.
    Make sure you update this again very soon.

  16. It could capitol roofing signal that there is no wonder that homeowners are becoming increasingly popular.
    Not inquiring about procedure – Plainly speaking, many roofing companies will always get
    customers from clients who have been screened for experience, quality and service.

    You are a busy person. The storm has been reported frequently along the Front Range over the past few decades, because of this one.

    Pay attention not just to their answers but, how they answer you as well.

    Tasks To

    31 August, 2014 at 9:01 PM

  17. I’m pretty pleased to find this great site.
    I need to to thank you for your time for this particularly wonderful read!!
    I definitely appreciated every little bit of it and I have you bookmarked to see new stuff in your
    blog.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: