Vitalie
Vitalie

Reputation: 413

Apply widget_title filter only to WordPress widgets from a certain sidebar?

I need to apply the widget_title filter only to the widgets from a certain sidebar. If the widget is from "footer" I want to apply:

function widget_title_empty($output='') {
    if ($output == '') {
        return ' ';
    }
    return $output;
}
add_filter('widget_title', 'widget_title_empty');

to it.

Thanks.

Upvotes: 5

Views: 3886

Answers (3)

Astrotim
Astrotim

Reputation: 2172

If you're looking to remove the widget title only, you can do this without creating a custom function by passing "__return_false" as the argument to the filter:

add_filter('widget_title', '__return_false');
dynamic_sidebar('footer');
remove_filter('widget_title', '__return_false');

See http://codex.wordpress.org/Function_Reference/_return_false

Upvotes: 1

webaware
webaware

Reputation: 2841

I like Calle's answer better, but this will work if you don't have those conditions:

class Example {

    protected $lastWidget = false;

    public function __construct() {
        add_filter('dynamic_sidebar_params', array($this, 'filterSidebarParams'));
    }

    /**
    * record what sidebar widget is being processed
    * @param array $widgetParams
    * @return array
    */
    public function filterSidebarParams($widgetParams) {
        $this->lastWidget = $widgetParams;

        return $widgetParams;
    }

    /**
    * check to see if last widget recorded was in sidebar
    * @param string $sidebarName
    * @return bool
    */
    public function isLastSidebar($sidebarName) {
        return $this->lastWidget && $this->lastWidget[0]['id'] == $sidebarName;
    }

}

$example = new Example();

// in your widget's code
if ($example->isLastSidebar('footer-widget-area')) {
    // you're in
}

Upvotes: 0

C. E.
C. E.

Reputation: 10607

Since you want to use a hardcoded sidebar name I assume perhaps you are creating a theme and have control over the code that prints the sidebar? If so, you could first add the filter and then remove it again, like this:

add_filter('widget_title', 'widget_title_empty');
dynamic_sidebar('footer');
remove_filter('widget_title', 'widget_title_empty');

This way, surrounding sidebars won't be affected.

Upvotes: 3

Related Questions