I have a number of rewrite rules set which output content correctly however I want to extend this so that the sidebars that appear on that rewritten page can also be amended so that a sidebar is called depending on the query_vars used. Is there anyway to manipulate what is rendered with dynamic_sidebar so we can query the query_vars and choose an appropriate sidebar to display? Or is it a case of wrapping dynamic_sidebar with tests to call the correct sidebar?

1 Answer 1


It's hard to tell your exact requirements, but testing to see which handle to pass to dynamic_sidebar is probably your best bet, since it'll allow you to administer each version from the WordPress back-end. Don't forget to register each version with register_sidebar

The other thing that might be what you're after is the sidebars_widgets filter, which lets you edit the global widgets array when it's accessed within dynamic_sidebar.

A sidebar is part of the template applied to the post/page, I can't see any way that changing it directly via a rewrite rule would make sense from a functional perspective.

  • I was overthinking this I think. What I wanted to achieve is to change the value used by dynamic_sidebar by accessing a filter when this function is called. (Now wondering whether it is worth possibly suggesting a filter to WP Core to amend this value at the beginning of that function) The simplest solution was to just use get_query_var and then call the relevant sidebar. I was trying to avoid overriding the template on which this appears by using a filter.
    – Zakalwe
    Commented Jun 20, 2017 at 12:47
  • What you're describing would normally be done by editing the template. I recently had to dynamically switch sidebars, based on taxonomy, on a theme myself and that was by far the simplest method. From what you're saying, it sounds like you might be taking a very convoluted approach to what you're attempting to do. Perhaps if you amend your question to describe exactly what you want to achieve from end to end? Commented Jun 20, 2017 at 17:51
  • Thanks Andrew. I've accepted your answer as I did just end up performing tests before passing an appropriate handle to dynamic_sidebar
    – Zakalwe
    Commented Jun 23, 2017 at 10:13

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.