2

I have several sidebars in the theme I'm developing. Some of them are designed to hold only a few widget types. So I'm trying to figure out how to keep other widgets out without using a plugin.

After a lot of googling, I believe there are only 2 options:

  1. Use jquery on widgets admin page to prevent dragging & dropping of certain widgets to certain widget areas. In a perfect world, the user will see some sort of message saying that this particular widget is not allowed in this particular widget area.
  2. Use wp 'sidebars_widgets' filter to remove widgets server side.

First sounds like a better way, mostly performance wise. What do you think? Although, I failed to find any working solution and my jquery knowledge is not enough. Sadly, my PHP knowledge obviously also is not enough to nail the sceond solution. Here's what I have:

add_filter( 'sidebars_widgets', 'wp150734_code_disable_widgets' );


function wp150734_code_disable_widgets( $sidebars_widgets ) {

    // This code works for one sidebar & one widget :
    $allowed_widget  = 'wp150734_widget_social';
    $sidebar_to_check = 'wp150734_widgets_navbar';

    // But I want to check the array of sidebars with allowed widgets:
    $sidebars_to_check = array(
        // allowed widgets in 'wp00734_widgets_navbar'
        'wp150734_widgets_navbar' => array(
            'widget_search',
            'widget_text',
            'wp150734_widget_social'
        ),
        // allowed widgets in 'wp00734_widgets_footer'
        'wp00734_widgets_footer' => array(
            'widget_search',
            'widget_text',
            'wp00734_widget_social'
        )
    );


    foreach ( $sidebars_widgets as $sidebars_key=>$sidebars_widget ) {

        // Check if we even need to check this sidebar
        if ( $sidebars_key != $sidebar_to_check )
        continue;

        if ( $sidebars_widget ) {

            foreach ( $sidebars_widget as $k=>$v ) {

                // Look for our custom widget and unset all other widgets from the $sidebars_widgets array
                if ( stripos( $v, $allowed_widget ) === false ) {
                    unset( $sidebars_widgets[$sidebars_key][$k] );
                }

            }
        }
    }

    return $sidebars_widgets;
};

What do you guys think? I'm feeling like jquery solution would be better, but can't find anything related.

EDIT: I have 2-3 custom widgets in addition to all the standard. I want all those widgets to work normally in all registered widget areas. Now I have a top navbar in the theme. It's designed to hold some data. Like menus, social icons, search and text in the chosen order. Since I already have all that in the form of widgets that can be reordered, it sounds like a logical idea to register a custom widget area in the navbar and let the user chose widgets and reorder them. No need to duplicate the functionality - widgets already offer it out of the box. The only problem is that not all widgets are designed to sit in the navbar.

1

You are overthinking it. The concept of widgets is designed to be transferable between widget area (AKA sidebars). If a widget is not tranferable it is just not a true wordpress widget.

Now creating your own framework of widgets sound like a non pleasant idea. In addition to fighting the wordpress core code, you will find yourself fighting established user expectations about widgets.

So you have basically two options, call it "Baitar's sidebars and widgets" to signal to the user they are not true wordpress widgets, and replicate the wordpress core code to manage them. Or just ask yourself why are you writing substandard widgets, and fix your design and code.

The third, and probably the easiest option, is just to let the user do whatever it wants and let him see by himself that the widgets do not work. Just provide proper documentation for that.

  • I see your point. And you're probably right. I just can't think of a better logic here. I'm not creating any widgets framework. Please, see the edit. I could use your advice and let it be like it is and just explain in the documentation. But the reality is that 99% of users do not bother reading the documentation. They will just complain that theme is broken because calendar widget doesn't look good in the navbar. – Nikita Mar 16 '16 at 9:15
  • @Baltar You see, they are likely to complain that the theme is broken also if they can not place a widget at the nav bar sidebar.... You can not really tell in advance which widget will and which will not work in your sidebar so if you will be too restrictive, you will prevent developer to write widgets specifically to work at that sidebar. – Mark Kaplun Mar 16 '16 at 9:36
  • You're right, this was a bad idea in the first place. I should find some other way to put that data in the navbar. Widgets are for sidebars. The fact that I was not able to find a proper solution anywhere across the net proves that once again. Thank you. Now, should I accept you answer? Or delete the question? – Nikita Mar 16 '16 at 12:29
  • @Baltar Accept ;) You are not the first one asking this kind of question and negative answers at least can give something to think about to people that will find this later. In general I think as long as the question was on topic, it is better to accept answers then to delete the question even if it turns out to be something impossible to do. – Mark Kaplun Mar 16 '16 at 12:36

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.