I’m writing a custom plugin that allows a particular page to have it’s own sidebar with widgets. It works great so far, but what I’d like to do is generalize it more and allow the plugin to “override” the dynamic sidebar of any page (if it has a custom sidebar) without having to edit any template files.

I’ve tried several hooks and have delved into the code a bit, but nothing stands our or works so far.

To save some time I thought I’d see if anybody has any working solutions to this issue? I might be able to solve this issue, but it is straining my “after working hours” brain.

If it isn’t possible I’d be more than willing to have a short-code or function call that does this for me, but I’d really like to make it idiot proof and just have it work without any template changes.

2 Answers
2

dynamic_sidebar() calls wp_get_sidebars_widgets() to get the list of all sidebars and their widgets. This output is filtered through sidebars_widgets, so you can modify it to add or remove widgets.

This array only contains widget IDs, so you need to register the widget instance too (it should end up in the global $wp_registered_widgets array). I think you can do this with wp_register_sidebar_widget(), but it is possible that this is older code from the time when a widget could not have multiple instances yet.

Leave a Reply

Your email address will not be published. Required fields are marked *