How to clean up wp_head() without a plugin

WordPress puts a lots of stuff via wp_head() hook. Some of this stuff is useful and some isn’t. You can easily remove this unnecessary stuff by adding a tiny code snippet to your functions.php file.

All you need to do is copy the following code and paste it within your functions.php file located inside your themes directory.

//clean wp_head() function
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'start_post_rel_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'adjacent_posts_rel_link' );
remove_action( 'wp_head', 'wp_shortlink_wp_head' );

Useful Read : Programmatically Create Categories in WordPress

3 Comments

  1. Hmm, this could be dangerous in the hands of those who don’t understand how fragile wp_head is. We should also warn users that wp_head should never be removed, and just because there’s cleanup, doesn’t guarantee that a plugin won’t insert it’s own junk into it at one point.

    For future reference, I found this other sites to explain what exactly is being removed in your snippet.

    remove_action( ‘wp_head’, ‘rsd_link’ );// remove really simple discovery link
    remove_action( ‘wp_head’, ‘wlwmanifest_link’ );// remove wlwmanifest.xml (needed to support windows live writer)
    remove_action( ‘wp_head’, ‘wp_generator’ );// remove wordpress version
    remove_action( ‘wp_head’, ‘start_post_rel_link’ );// remove random post link
    remove_action( ‘wp_head’, ‘index_rel_link’ );// remove link to index page
    remove_action( ‘wp_head’, ‘adjacent_posts_rel_link’ );// remove the next and previous post links
    remove_action( ‘wp_head’, ‘wp_shortlink_wp_head’ ); //Inject rel=shortlink into head if a shortlink is defined for the current page

    Also some people might want to keep the adjacent_posts_rel_link so users should be wary of what they’re removing before dropping in code snippets. Hope it helps and thanks for the snippet! 🙂

    Reply

Leave a Comment.