How to remove inline Recent Comments style from your WordPress Theme

Have you seen the following code inside the HEAD section of your WordPress blog source code?

<style type="text/css">.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}</style>

Ever wonder how it got there, and how to get rid of it?

The inline style gets injected into the head section when you use the Recent Comments widget. The widget applies a filter to wp_head() when it is activated.

In order to get rid of the inline style, short of modifying the core file (not advised), is to apply a filter of our own. Let’s get to work on that.

Move the styling to your theme style.css file

Copy the following code to your theme css file:

.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}

Once you have the code inside your style.css file, you can edit it to your hearts content.

Add a filter to remove a filter

Yep, seems a little crazy, but we are going to add a filter to remove the stock WordPress filter.

Add the following to your theme functions.php file:

function remove_wp_widget_recent_comments_style() {
   if ( has_filter('wp_head', 'wp_widget_recent_comments_style') ) {
      remove_filter('wp_head', 'wp_widget_recent_comments_style' );
   }
}
add_filter( 'wp_head', 'remove_wp_widget_recent_comments_style', 1 );

Here we are adding a filter via the WordPress add_filter() method. The first argument, wp_head, which is the hook where the style filter gets applied in the first place.

The second argument is the callback to our custom filter, remove_wp_widget_recent_comments_style().

The third parameter is the priority of when our filter will get processed. We are setting this to 1 to insure that it takes priority over the WordPress core function. If this argument is too higher of a number, then we run the risk of our filter not taking precedence.

Our custom filter is simple, it first checks to see if the WordPress filter is present. If it is present, then we simply remove the filter. The remove_filter() function takes the same arguments as add_filter()

Some thoughts

While the inline style does not hurt anything, some purists just hate seeing it there in the first place. It is one of those quirky things that WordPress code does to you.

The remove_filter() WordPress function can come in handy for other things that bug the heck out of you. One example would be the curly quotes that replace normal quotes inside of your posts and pages.

You can remove the wptexturize filter from any of the following hooks for example:

remove_filter('category_description', 'wptexturize');
remove_filter('list_cats', 'wptexturize');
remove_filter('comment_author', 'wptexturize');
remove_filter('comment_text', 'wptexturize');
remove_filter('single_post_title', 'wptexturize');
remove_filter('the_title', 'wptexturize');
remove_filter('the_content', 'wptexturize');
remove_filter('the_excerpt', 'wptexturize');

You can think of remove_filter is to WordPress, as reset.css is to style sheets. Wonderful world it is.

Share with the world!
  • E-mail this story to a friend!
  • TwitThis
  • Digg
  • StumbleUpon
  • del.icio.us
  • Ping.fm
  • Reddit

16 comments ↓

Leave a Comment