<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>beer planet &#187; widget</title> <atom:link href="http://beerpla.net/tag/widget/feed/" rel="self" type="application/rss+xml" /><link>http://beerpla.net</link> <description>where things have nothing to do with beer - tutorials, tips, how-tos, thoughts, hacks, and other techy nonsense</description> <lastBuildDate>Thu, 17 May 2012 22:50:53 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <atom:link rel='hub' href='http://beerpla.net/?pushpress=hub'/> <item><title>How To Remove Inline Hardcoded Recent Comments Sidebar Widget Style From Your WordPress Theme</title><link>http://beerpla.net/2010/01/31/how-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme/</link> <comments>http://beerpla.net/2010/01/31/how-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme/#comments</comments> <pubDate>Sun, 31 Jan 2010 09:45:15 +0000</pubDate> <dc:creator>Artem Russakovskii</dc:creator> <category><![CDATA[Tips]]></category> <category><![CDATA[Wordpress]]></category> <category><![CDATA[add_action]]></category> <category><![CDATA[comments]]></category> <category><![CDATA[functions]]></category> <category><![CDATA[functions.php]]></category> <category><![CDATA[hardcoded]]></category> <category><![CDATA[inline]]></category> <category><![CDATA[recent]]></category> <category><![CDATA[recent_comments_style]]></category> <category><![CDATA[remove]]></category> <category><![CDATA[remove_action]]></category> <category><![CDATA[sidebar]]></category> <category><![CDATA[theme]]></category> <category><![CDATA[widget]]></category> <guid
isPermaLink="false">http://beerpla.net/2010/01/31/how-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme/</guid> <description><![CDATA[<h2>The Problem</h2><p>If you use the Recent Comments sidebar widget in your WordPress installation, it&#039;s possible that you want to customize this widget&#039;s style.</p><p>You will quickly find, however, that as soon as you add the widget to your sidebar, it injects the following inline, hardcoded CSS into the containing page (using !important to make things worse):&#160;</p><div
class="wp_syntax"><div
class="code"><pre>&#60;style type=&#34;text/css&#34;&#62;.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}&#60;/style&#62;</pre></div></div><p>The code above comes from <strong><em>recent_comments_style()</em></strong> (found in wp-includes/default-widgets.php), which is in turn called by <strong><em>WP_Widget_Recent_Comments()</em></strong> in the same file (this is just an old-style PHP4 constructor &#8211; same as PHP5&#039;s __construct()), which is triggered when the Recent Comments widget is used:</p><div
class="wp_syntax"><div
class="code"><pre>add_action( 'wp_head', array(&#38;$this, 'recent_comments_style') );</pre></div></div><p>This leaves a bad taste in my mouth because:</p><ul><li>the</li>...<div
class=clear></div> <a
href="http://beerpla.net/2010/01/31/how-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme/" class="read_more"><div
class=excerpt-end>Read the rest of this article &#187;</div></a></ul>]]></description> <content:encoded><![CDATA[<h2>The Problem</h2><p>If you use the Recent Comments sidebar widget in your WordPress installation, it&#039;s possible that you want to customize this widget&#039;s style.</p><p>You will quickly find, however, that as soon as you add the widget to your sidebar, it injects the following inline, hardcoded CSS into the containing page (using !important to make things worse):&#160;</p><div
class="wp_syntax"><div
class="code"><pre>&lt;style type=&quot;text/css&quot;&gt;.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}&lt;/style&gt;</pre></div></div><p>The code above comes from <strong><em>recent_comments_style()</em></strong> (found in wp-includes/default-widgets.php), which is in turn called by <strong><em>WP_Widget_Recent_Comments()</em></strong> in the same file (this is just an old-style PHP4 constructor &#8211; same as PHP5&#039;s __construct()), which is triggered when the Recent Comments widget is used:</p><div
class="wp_syntax"><div
class="code"><pre>add_action( 'wp_head', array(&amp;$this, 'recent_comments_style') );</pre></div></div><p>This leaves a bad taste in my mouth because:</p><ul><li>the style is automatically forced into any page that embeds the widget</li><li>it uses !important to override any existing styles</li><li>it cannot be configured from the widget</li><li>this is a core WordPress widget and not some widget added by a random plugin. WordPress should not contain such bad practices.</li></ul><p>After seeing this, I raised a new <a
href="http://core.trac.wordpress.org/ticket/11928" rel="nofollow">issue #11928</a> in the WordPress bug tracker but in the meantime, of course, I could go and hack the default-widgets.php core file to solve this problem. That, of course, would be a horrible thing to do &#8211; the next upgrade could wipe my changes or result in a conflict (if upgrading via svn). In general, one should never need to modify core files.</p><h2>The Solution</h2><p>Fortunately, there is a solution that doesn&#039;t involve modifying core files. Remember that we are talking about WordPress here, which gained its popularity due to the ease of customization.</p><p>Normally, we would just need to remove the <strong><em>recent_comments_style</em></strong> hook, which would prevent it from ever firing and injecting the CSS. In fact, <a
href="http://webstractions.com/wordpress/remove-recent-comments-inline-styl/" rel="nofollow">somebody over at webstractions.com</a> already took a crack at this approach with the following code, which may have worked for a while but doesn&#039;t anymore:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td
class="code"><pre>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 );</pre></td></tr></table></div><p>This code doesn&#039;t work in the current version of WordPress because <strong><em>recent_comments_style()</em></strong> belongs to the <strong><em>WP_Widget_Recent_Comments</em></strong> class, and we need a tiny bit more effort to get through to the object of that class.</p><p>The following snippet, suggested by <a
href="http://twitter.com/nacin" rel="nofollow">Andrew Nacin</a> in a comment to the bug I opened up, does the job and works in WordPress 2.9.1:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
</pre></td><td
class="code"><pre>add_action('widgets_init', 'my_remove_recent_comments_style');
function my_remove_recent_comments_style() {
	global $wp_widget_factory;
	remove_action('wp_head', array($wp_widget_factory-&gt;widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
}</pre></td></tr></table></div><p>This code first gets access to the right object and then passes it to the <strong><em>remove_action()</em></strong> function, along with the right function name <strong><em>recent_comments_style</em></strong>. Stick it intoyour functions.php and it should do the trick.</p><p>And there you have it.</p><div
class="shr-bookmarks shr-bookmarks-expand"><ul
class="socials"><li
class="shr-twitter"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Remove+Inline+Hardcoded+Recent+Comments+Sidebar+Widget+Style+From+Your+WordPress+Theme&amp;link=http://beerpla.net/2010/01/31/how-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme/&amp;notes=The%20Problem%20%20If%20you%20use%20the%20Recent%20Comments%20sidebar%20widget%20in%20your%20Wordpress%20installation%2C%20it%27s%20possible%20that%20you%20want%20to%20customize%20this%20widget%27s%20style.%20%20You%20will%20quickly%20find%2C%20however%2C%20that%20as%20soon%20as%20you%20add%20the%20widget%20to%20your%20sidebar%2C%20it%20injects%20the%20following%20inline%2C%20hardcoded%20CSS%20into%20the%20contai&amp;short_link=http://bit.ly/aafYTI&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a></li><li
class="shr-facebook"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Remove+Inline+Hardcoded+Recent+Comments+Sidebar+Widget+Style+From+Your+WordPress+Theme&amp;link=http://beerpla.net/2010/01/31/how-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme/&amp;notes=The%20Problem%20%20If%20you%20use%20the%20Recent%20Comments%20sidebar%20widget%20in%20your%20Wordpress%20installation%2C%20it%27s%20possible%20that%20you%20want%20to%20customize%20this%20widget%27s%20style.%20%20You%20will%20quickly%20find%2C%20however%2C%20that%20as%20soon%20as%20you%20add%20the%20widget%20to%20your%20sidebar%2C%20it%20injects%20the%20following%20inline%2C%20hardcoded%20CSS%20into%20the%20contai&amp;short_link=http://bit.ly/aafYTI&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-googlebuzz"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Remove+Inline+Hardcoded+Recent+Comments+Sidebar+Widget+Style+From+Your+WordPress+Theme&amp;link=http://beerpla.net/2010/01/31/how-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme/&amp;notes=The%20Problem%20%20If%20you%20use%20the%20Recent%20Comments%20sidebar%20widget%20in%20your%20Wordpress%20installation%2C%20it%27s%20possible%20that%20you%20want%20to%20customize%20this%20widget%27s%20style.%20%20You%20will%20quickly%20find%2C%20however%2C%20that%20as%20soon%20as%20you%20add%20the%20widget%20to%20your%20sidebar%2C%20it%20injects%20the%20following%20inline%2C%20hardcoded%20CSS%20into%20the%20contai&amp;short_link=http://bit.ly/aafYTI&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a></li><li
class="shr-reddit"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Remove+Inline+Hardcoded+Recent+Comments+Sidebar+Widget+Style+From+Your+WordPress+Theme&amp;link=http://beerpla.net/2010/01/31/how-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme/&amp;notes=The%20Problem%20%20If%20you%20use%20the%20Recent%20Comments%20sidebar%20widget%20in%20your%20Wordpress%20installation%2C%20it%27s%20possible%20that%20you%20want%20to%20customize%20this%20widget%27s%20style.%20%20You%20will%20quickly%20find%2C%20however%2C%20that%20as%20soon%20as%20you%20add%20the%20widget%20to%20your%20sidebar%2C%20it%20injects%20the%20following%20inline%2C%20hardcoded%20CSS%20into%20the%20contai&amp;short_link=http://bit.ly/aafYTI&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a></li><li
class="shr-hackernews"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Remove+Inline+Hardcoded+Recent+Comments+Sidebar+Widget+Style+From+Your+WordPress+Theme&amp;link=http://beerpla.net/2010/01/31/how-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme/&amp;notes=The%20Problem%20%20If%20you%20use%20the%20Recent%20Comments%20sidebar%20widget%20in%20your%20Wordpress%20installation%2C%20it%27s%20possible%20that%20you%20want%20to%20customize%20this%20widget%27s%20style.%20%20You%20will%20quickly%20find%2C%20however%2C%20that%20as%20soon%20as%20you%20add%20the%20widget%20to%20your%20sidebar%2C%20it%20injects%20the%20following%20inline%2C%20hardcoded%20CSS%20into%20the%20contai&amp;short_link=http://bit.ly/aafYTI&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=202&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to Hacker News">Submit this to Hacker News</a></li><li
class="shr-delicious"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Remove+Inline+Hardcoded+Recent+Comments+Sidebar+Widget+Style+From+Your+WordPress+Theme&amp;link=http://beerpla.net/2010/01/31/how-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme/&amp;notes=The%20Problem%20%20If%20you%20use%20the%20Recent%20Comments%20sidebar%20widget%20in%20your%20Wordpress%20installation%2C%20it%27s%20possible%20that%20you%20want%20to%20customize%20this%20widget%27s%20style.%20%20You%20will%20quickly%20find%2C%20however%2C%20that%20as%20soon%20as%20you%20add%20the%20widget%20to%20your%20sidebar%2C%20it%20injects%20the%20following%20inline%2C%20hardcoded%20CSS%20into%20the%20contai&amp;short_link=http://bit.ly/aafYTI&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-stumbleupon"> <a
href="http://www.shareaholic.com/api/share/?title=How+To+Remove+Inline+Hardcoded+Recent+Comments+Sidebar+Widget+Style+From+Your+WordPress+Theme&amp;link=http://beerpla.net/2010/01/31/how-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme/&amp;notes=The%20Problem%20%20If%20you%20use%20the%20Recent%20Comments%20sidebar%20widget%20in%20your%20Wordpress%20installation%2C%20it%27s%20possible%20that%20you%20want%20to%20customize%20this%20widget%27s%20style.%20%20You%20will%20quickly%20find%2C%20however%2C%20that%20as%20soon%20as%20you%20add%20the%20widget%20to%20your%20sidebar%2C%20it%20injects%20the%20following%20inline%2C%20hardcoded%20CSS%20into%20the%20contai&amp;short_link=http://bit.ly/aafYTI&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="shr-mail"> <a
href="http://www.shareaholic.com/api/share/?title=How%20To%20Remove%20Inline%20Hardcoded%20Recent%20Comments%20Sidebar%20Widget%20Style%20From%20Your%20WordPress%20Theme&amp;link=http://beerpla.net/2010/01/31/how-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme/&amp;notes=The%20Problem%20%20If%20you%20use%20the%20Recent%20Comments%20sidebar%20widget%20in%20your%20Wordpress%20installation%2C%20it%27s%20possible%20that%20you%20want%20to%20customize%20this%20widget%27s%20style.%20%20You%20will%20quickly%20find%2C%20however%2C%20that%20as%20soon%20as%20you%20add%20the%20widget%20to%20your%20sidebar%2C%20it%20injects%20the%20following%20inline%2C%20hardcoded%20CSS%20into%20the%20contai&amp;short_link=http://bit.ly/aafYTI&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=201&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a></li></ul><div
style="clear: both;"></div></div> Similar Posts:<ul><li><a
href="http://beerpla.net/2012/04/13/how-to-fix-incomplete-wordpress-wxr-exports/" rel="bookmark" title="April 13, 2012">How To Fix Incomplete WordPress (WXR) Exports</a></li><li><a
href="http://beerpla.net/2010/10/15/how-to-add-shortcode-support-to-wordpress-comments-and-sidebars/" rel="bookmark" title="October 15, 2010">How To Add Shortcode Support To WordPress Comments And Sidebar Widgets</a></li><li><a
href="http://beerpla.net/2010/01/13/wordpress-plugin-development-how-to-include-css-and-javascript-conditionally-and-only-when-needed-by-the-posts/" rel="bookmark" title="January 13, 2010">[WordPress Plugin Development] How To Include CSS and JavaScript Conditionally And Only When Needed By The Posts</a></li><li><a
href="http://beerpla.net/2008/03/29/beer-planet-upgraded-to-wordpress-25/" rel="bookmark" title="March 29, 2008">Beer Planet Upgraded To WordPress 2.5</a></li><li><a
href="http://beerpla.net/2010/11/06/how-to-dynamically-increase-memory-limits-when-interfacing-with-wordpress-using-xml-rpc-windows-live-writer-etc/" rel="bookmark" title="November 6, 2010">How To Dynamically Increase Memory Limits When Interfacing With WordPress Using XML-RPC (Windows Live Writer, Etc)</a></li></ul><p><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbeerpla.net%2F2010%2F01%2F31%2Fhow-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme%2F&amp;title=How%20To%20Remove%20Inline%20Hardcoded%20Recent%20Comments%20Sidebar%20Widget%20Style%20From%20Your%20WordPress%20Theme" id="wpa2a_2"><img
src="http://beerpla.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded> <wfw:commentRss>http://beerpla.net/2010/01/31/how-to-remove-inline-hardcoded-recent-comments-sidebar-widget-style-from-your-wordpress-theme/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> </channel> </rss>
