3

After searching under the tag and Google the closest I have found that populates what I am looking for is this article: Is there a way to allow only certain type of widgets in the sidebars?

I also found this page: How to add a specific widget to only 1 page?.

I reviewed the codex for dynamic_sidebar() and register_sidebar() but I do not see a way when someone goes to the Widgets Panel and under the Available Widgets area you can limit what location a widget can be applied to.

Question: If I want the Tag Cloud widget to only be allowed in a certain registered sidebar, in this case foobar where do I restrict what is allowed?

// Custom widget area for foobar.
if ( function_exists( 'register_sidebar' ) ) {
    register_sidebar( array(
        'name'              => __( 'Foobar Sidebar', 'text_domain'),
        'id'                => 'widget-foobar',
        'description'       => __( 'A custom widget area for foobar', 'text_domain' ),
        'before_widget'     => 'some-code',
        'after_widget'      => 'some-code',
        'before_title'      => 'some-code',
        'after_title'       => 'some-code',
    )); 
}

Or how I can limit custom and default widgets for certain areas I've registered?

2
  • The first question you linked to answers what you need to do exactly. I just tested and it works perfectly.
    – Cai
    Feb 5, 2016 at 10:31
  • I'm looking for a solution in pure PHP that isn't after the fact and can be applied on the widget level not the admin level. I don't mind using JavaScript but I was looking for a better option and to see if there was an updated solution to the core I wasn't seeing since that question was three years old.
    – user9447
    Feb 5, 2016 at 14:39

1 Answer 1

1

I don't think it is possible in a nice user friendly way. Wordpress admin screen and the customizer assume that any widget can get into any sidebar and will show them as part of the admin, so even if you filter out the widgets you do not want, it will probably create a lot of WTF moments for the user.

I would just suggest not to go that way. If you need to restrict the widgets and you can not count on the user to have self control, then the sidebar/widgets combo might just not be a good answer for what you try to do.

2
  • I did use unregister_widget() for widgets I do not want to support. So what do people usually do when they modify a default widget for a certain area when it wont look very good in other locations? For example, I modified the tag cloud widget to work well with the footer but not anywhere else.
    – user9447
    Jan 29, 2016 at 15:52
  • 1
    @Darth_Vader IMO, once you documented it, it is the responsibility of the user to follow you recommendations.You can make the name/description of the sidebar to include some warning about it, but do you realy sure that all other widgets will break there? If this is not something that will break the site why not to let the site admin put any widget he wants and let him adjust CSS and JS if he wants to make it compatible. If you want to target CSS and JS to only apply to the one widget type at a specific sidebar, then just let your widgets have some unique class. Jan 29, 2016 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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