Is it possible to include or exclude specific named widgets that are assigned to a named dynamic_sidebar call?
For example, if I’ve registered a sidebar named “my_sidebar” and the user had placed a “Links” widget into it, I want to be able to include or exclude it based on a custom setting in my theme options panel.
Is this possible?
Any insights much appreciated.
dynamic_sidebar()
callswp_get_sidebars_widgets()
to get all widgets per sidebar. I think filtering this output is the best way to remove a widget from an sidebar.I am adding another answer to answer this question:- How to exclude certain widget from showing up on home/front page?
WordPress has an internal function
_get_widget_id_base()
I am not sure how safe to use it. But this is the method which WordPress uses to get widget ID base by using widget ID instead ofstrpos()
andstrncmp()
.Example:-
Extending Jan’s answer, I found
strpos()
rather thanstrncmp()
for checking the widget name(it’s faster.. )
Following you’ll find a similar function (working & tested) that will take you to the same result: