1

I have 2 registered sidebars in my theme one for Products and one for Blog.

What I am trying to do is if my "Category" widget is added to the Product Sidebar the taxonomy is automatically set to "product_cats" and if added to the Blog Sidebar set the taxonomy to "category". This will allow my widget on the backend to update which categories it displays in the dropdown menu.

I have been searching the wp-includes/widgets.php file for a method or property that contains the id or name of the Parent Sidebar of the widget but can't seem to find a way to do this.

If I can get the name or id of the Parent Sidebar I would do something like this in the form method of my widget:

public function form( $instance ) {

    $parent = getParentSidebarID($this); // How do you do this!

    switch ($parent) {
        case 'product-sidebar':
            $tax = 'product_cat';
            break;
        case 'blog-sidebar':
            $tax = 'category';
            break;
        default:
            $tax = '';
            break;
    }
}

Is this possible?

3

There isn't a standard way to do it AFAIK in the form() method; here's a function to do it:

function wp158055_get_sidebar_id( $widget ) {
    foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) {
        if ( array_search( $widget->id, $widget_ids ) !== false ) return $sidebar_id;
    }
    return false;
}

And call with $this. Note from the widget() method you can use $args['id'] as mentioned in this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.