Cleaning Up WordPress Themes

WordPress LogoBy default WordPress adds all sorts of code between the opening and closing head tags of a WordPress theme. Most themes also display notes below a comment form and solicit the commenter’s website address. You may want to get rid of all that so here’s how you do it.

Cleaning up Theme Header

There are various links added to a WordPress theme’s head section and here’s how you can remove them:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function clean_head() {
    //category feeds
    remove_action( 'wp_head', 'feed_links_extra', 3 ); 
 
    //post and comments feed
    remove_action( 'wp_head', 'feed_links', 2 ); 
 
    //only required if you are looking to blog using an external tool
    remove_action( 'wp_head', 'rsd_link'); 
 
    //something to do with windows live writer
    remove_action( 'wp_head', 'wlwmanifest_link');
 
    //next previous post links
    remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); 
 
    //generator tag
    remove_action( 'wp_head', 'wp_generator'); 
 
    //short links like ?p=124
    remove_action( 'wp_head', 'wp_shortlink_wp_head'); 
}
 
clean_head();

You will have noticed that on line 6 we are removing the link to the post RSS feed as well. That is because feed_links() adds both the comments and posts feeds. If you want both of those then comment out that line. If you want just the posts feed then keep that line in and add the following too:

1
2
3
4
5
add_action( 'wp_head', 'add_default_feed_link' );
 
public function add_default_feed_link() {
       echo "<link rel='alternate' type='application/rss+xml' title='" . get_bloginfo('name') . " &raquo; Feed' href='" . get_feed_link() . "' />";
}

The above will re-add the posts RSS feed.

Remove Website Field From WordPress Theme Comment Form

To remove the comment author website field from themes’ comment forms you can add the following code to your theme’s functions.php file or use the plugin I’ve provided at the end:

1
2
3
4
5
6
7
8
9
10
11
add_filter( 'comment_form_defaults', 'clean_comment_form' );
 
function clean_comment_form( $defaults ) {
        //remove html code hints below comment message form
        $defaults[ 'comment_notes_after' ] = '';
 
        //remove "website" field
        $defaults[ 'fields' ][ 'url' ] = '';
 
        return $defaults;
}

We filter the comment_form_defaults and then remove the after comment form message and the URL field.

WordPress Plugin

Since I wanted to make all of the above changes to a number of different themes I’ve created a WordPress plugin called Theme Cleaner. You can download it from below.

Install and activate it on any website where you want to make these changes.

2 thoughts on “Cleaning Up WordPress Themes

  1. Thank you for the post, very helpful.

    If I want to use your plugin but do NOT want both the comments and posts feeds, what changes do I have to make?

    Thank you.

    • Sorry for the late reply. I missed your comment. Don’t add the add_default_feed_link function to wp_head. See the 3rd paragraph of the post above.

Leave a Reply

Your email address will not be published. Required fields are marked *