$30
How can I use filters to add unique classes to my rss widgets?
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?
Darren Hoyt | 12/08/09 at 11:31am
| Edit
(8) Possible Answers Submitted...
-

Last edited:
12/08/09
12:26pmUtkarsh Kukreti says: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'); -

Last edited:
12/11/09
2:56pmRon Rennick says: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'); -

Last edited:
12/08/09
1:14pmRon Rennick says: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'])) { -

Last edited:
12/09/09
9:51amMichael Hampton says: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. -

Last edited:
12/09/09
10:07amMichael Hampton says:I should amend that: the correct function name is register_sidebars(). You can call it as many times as you need to.
-

Last edited:
12/11/09
2:56pmTim Holt says: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
(a class that belongs to the anchor) suggests that you're aiming to replaceclass='rsswidget'
with<a class='rsswidget'>
.<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
but<a 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):<div class='rsswidget'>
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');
Hope that does what you want it to do.
- Tim -

Last edited:
12/09/09
10:46amTim Holt says:Hmm, the answer above isn't formatted as I would have liked, but hopefully it's clear enough. An "edit" option would be nice...
-

Last edited:
12/10/09
7:16pmbetatea says: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">
This question has expired.
Current status of this question: Completed




