Archived topic
Conditional widgets in custom widget area
10 replies · Started by Michel on November 1, 2022
Hi,
I have created a custom widget area to display different custom navigation menus in various sections of my site.
I used a plugin ("Conditional Widgets") to display them conditionaly (only when in specific "sections" of the site).
Following the block-based widget management update, this plugin no longer works.
I am trying to manually recreate the same kind logic with Elements (using a Block element for instance), which allows me to fine tune display conditions, but I fail to find a way to output the block in the correct widget area. Is there a way to achieve that?
Hi there,
Elements only work with Hooks.
Before we look at alternative options - you can disable block widgets, so that plugin will work:
https://docs.generatepress.com/article/disable-the-block-editor-in-widgets/
is that an option. Let me know.
Hi David,
Thank you for your quick reply and insightful tip.
Moving back to old widgets might be a temprary solution (although I would have to check that going backwards won't create other side effects), but I don't think it is a good idea to use that in the long term, either because wordpress (or plugins / themes) will stop supporting "old widgets" altogeher, or because this specific plugin is no longer maintained, and will become incompatible with newer wwordpress versions (or newer php versions, etc.). So I would rather find a more solid GeneratePress "native" solution, even if that involves some manual coding.
I understand that Elements are triggered by hooks, and I managed to conditionaly output a block above the main content for instance. But I cannot seem to find a hook that triggers when a custom widget area (or custom sidebar) is being generated / showed on the page.
What are the Custom Widgets areas ? And how are they being added ?
I created one custom widget area (that I called content_top) using register_sidebar() in the functions.php of my child theme.
I also created a hook Element that just calls dynamic_sidebar for that custom area on specific pages.
The widgets (various menus) were all added to that custom area throught the old widget interface, and displayed conditionaly using this Conditional Widgets plugin, which allows to specify display conditions at the individual widget level.
Are there other widgets in those areas? Or are they just there for the navigaiton menus?
Only for navigation menus
Ok. If i understand correctly, and forgive me i am having a day of brain fogginess lol
1. when you registered your custom widget areas you would have hooked them in ( add_action ) to some hook.
2. you can create a block element and set the Hook to the above hook.
3. Add a nav block to the element
4. Set the Display Rules to places you need it displayed
Let me know
Thanks David.
Not sure I fully understand your suggestion though.
Whatever I do to conditionnaly display a block Element, I have no way to make it appear inside that custom widget area (cwa).
Sure I can use the same hook I used to display the cwa (generate_before_main_content in my case). But it will show either above or below the custom widgets area, not within.
If you were suggesting that actually use the same hook I used to register in my functions.php (widgets_init), I doubt it would work at all, and there is still the same fundamental issue that I cannot execute code (or trigger an Element) inside the cwa.
Or am I missing something here?
… or maybe you are suggesting that I get rid of the cwa altogeter, and just directly output the menu block at the same place where the cwa was displayed?
That would be a possibility, since that cwa is used only for these specific menus. All I will need is to refactor my CSS.
Thank you for your help.
Michel
… or maybe you are suggesting that I get rid of the cwa altogeter, and just directly output the menu block at the same place where the cwa was displayed?
Thats correct.
Let us know if you need any help with styling.