Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How can I use filters to add unique classes to my rss widgets? WordPress

  • SOLVED

For each RSS widget I'd like to add a unique class like this, but it doesn't seem to work:

function edit_anchor($anchor) {
return preg_replace('/<div class='rsswidget'/', '<div class='special_name'', $anchor, 1);
}
add_filter('wp_widget_rss','edit_anchor');

Is there a better way?

Answers (8)

2009-12-08

Ron Rennick answers:

This may work:

function edit_anchor($params) {
if(is_array($params) && is_array($params[0]) && isset($params[0]['before_widget']) {
$params[0]['before_widget'] = preg_replace('/<div class=\'rsswidget\'/', '<div class=\'special_name\'', $params[0]['before_widget'], 1);
}
return $params;
}
add_filter('dynamic_sidebar_params','edit_anchor');

2009-12-09

Tim Holt answers:

I'm not 100% clear on where you're trying to add class='special_name'.

The fact that you've named your function "edit_anchor", and that your preg_replace call replaces class='rsswidget' (a class that belongs to the anchor) suggests that you're aiming to replace <a class='rsswidget'> with <a class='special_name'>.

Unfortunately that can't be done using a filter. In the latest version of WordPress (v2.8.6) the anchor tag with the 'rsswidget' class is added to the title in line 728 of wp-includes/default-widgets.php. This is then echoed in line 732 without any filter (or action) being called inbetween. That means that there's no opportunity to modify the anchor class before it's output to the browser.

However, in your code snippet your preg_replace call doesn't replace <a class='rsswidget'> but <div class='rsswidget'>, so perhaps it's the class of the container div that you're looking to replace. If so, then that can be done in roughly the way Ron_R suggested (I'm reworked his code slightly to make it work with more themes):

<blockquote>function edit_widget_rss_class($params) {
if(is_array($params) && is_array($params[0]) && isset($params[0]['before_widget'])) {
$params[0]['before_widget'] = preg_replace('/widget_rss/', 'special_name', $params[0]['before_widget'], 1);
}
return $params;
}
add_filter('dynamic_sidebar_params','edit_widget_rss_class');</blockquote>

Hope that does what you want it to do.

- Tim

2009-12-08

Utkarsh Kukreti answers:

Try this
function edit_anchor($anchor) {

return preg_replace('/<a class=\'rsswidget/', '<a class=\'special_name', $anchor, 1);

}

add_filter('wp_widget_rss','edit_anchor');

2009-12-08

Ron Rennick answers:

I couldn't see a link to edit my answer - It is missing a parenthesis:

if(is_array($params) && is_array($params[0]) && isset($params[0]['before_widget']) {

should have been

if(is_array($params) && is_array($params[0]) && isset($params[0]['before_widget'])) {

2009-12-09

Michael Hampton answers:

The markup you're trying to filter doesn't actually get passed to the filter. Instead, it's located in the $before_widget variable pased to register_sidebar() by your theme.

The best way (though by no means elegant) to do this is to have your theme register multiple sidebars with slightly different $before_widget content. In your theme sidebar.php, or a file that's included from it, you should find the register_sidebar code. You will need to register unique new sidebars for each one that you want to appear differently. Copy the register_sidebar() code, make the desired changes, and make sure your sidebar.php now calls dynamic_sidebar() for each new sidebar you've created. Place a single RSS widget in each sidebar, and arrange the registered sidebars (in your theme sidebar.php) however you like.

2009-12-09

Michael Hampton answers:

I should amend that: the correct function name is register_sidebars(). You can call it as many times as you need to.

2009-12-09

Tim Holt answers:

Hmm, the answer above isn't formatted as I would have liked, but hopefully it's clear enough. An "edit" option would be nice...

2009-12-11

betatea answers:

Do you really need a unique class if you can address each of your rss widgets with their unique id?

#rss-4 in the following example:

<li id="rss-4" class="widget widget_rss">