3

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.

3
  • so you want a sidebar that if it has items then use this one else use a different one Commented May 20, 2011 at 5:18
  • That is correct! I have sidebars already registered, and would like to have the widgets inside of those displayed based on the page a user is on. The code is all there, I just need a way to override the dynamic_sidebar() function. Right now the base code works, but is dependent on the template, id just like to figure a way to override it so I can change it in the plugin rather than the template. I'm not even sure this is possible, to be honest.
    – dpgtfc
    Commented May 20, 2011 at 5:24
  • do these pages have something in common? you could always use a custom field with a some if and else statement Commented May 20, 2011 at 5:33

2 Answers 2

3

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.

2
  • Thanks, I ended up going this route and it worked great.
    – dpgtfc
    Commented May 27, 2011 at 16:05
  • @dpgtfc: Great that it worked for you! Can you mark this answer as accepted? Then the question will not stay around as "unanswered".
    – Jan Fabry
    Commented May 27, 2011 at 16:07
1

Yes, its totally possible, but not the easiest thing that can be done. It took me about two months to perfect it when I was working on Total Widget Control, but I was able to do it.

It's not a direct override tho, what happens is that dynamic_sidebar loads the $registered_widgets global and loops through the widgets, triggering the callback for each of these widgets.

  1. So what I ended up doing was grabbing this registered_widgets global, backing it up to my own global and then replacing the original callbacks with an empty callback function.

    This way dynamic_sidebars was still called, but it doesn't actually do anything.

  2. The next step was a bit trickier and took a bit of debugging. I had to take a total count of the original widgets for the given sidebar, and then keep track of what loop that dynamic_sidebars was on. When dynamic_sidbars reached its last widget, I called my own do_action('trigger_new_sidebar').

This basically allows me to start adding actions to this trigger and displaying any widgets that I wanted to display.

1
  • 1
    Thanks! I ended up doing something similar and finished my plugin, as you can see in the code I used the sidebars_widget filter and either added to sidebar-1 or replaced it with the page's custom sidebar.
    – dpgtfc
    Commented May 27, 2011 at 16:00

Your Answer

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

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