Iâd like to have a plugin for a flexible sidebar/widget management. In fact, I want to give a user the possibility to choose which sidebar should be displayed per page/post.
I know there are many sidebar plugins out there. Unfortunately, I havenât found one which cover all my wishes. So Iâd like to code this on my own. Which of the following possibilities do you find is the best and most flexible? Or do you have other approaches?
Posibilities
-
Hardcode the register_sidebar calls in a plugin/functions.php and add a metabox for the
post_edit.php
,post_new.php
screens.- Not that flexible. If a user has 100 pages and every page should have a different sidebar, the user has to register additional sidebars manually.
-
Same as Nr.1 but use a generic sidebar template with conditional tags to display the widgets. We could also use the plugin Widget Logic for a more granular filtering in the backend.
- Good for a programmer or experienced WordPress user, but not for a normal user. The conditional tags must be mapped in a friendly manner. Also
is_page(id)
could create problems on multilingual sites, where posts of a different language are stored with another id.
- Good for a programmer or experienced WordPress user, but not for a normal user. The conditional tags must be mapped in a friendly manner. Also
-
A flexible-sidebars Custom Post Type. Every post in this CPT is used in a generic sidebar-template to display the given content. The sidebar is also selected within a custom metabox. The user can add content to the sidebar with the default WYSIWYG editor which is a big plus.
- Not a ârealâ sidebar. Therefore widgets couldnât longer be added on the widgets-subpage in the admin panel. We have to create an own metabox for widget-assignments.
-
Modify the widgets-subpage in the admin panel to create/register your own sidebars in WordPress. Also create a generic widget with a default WYSIWYG editor. Content with attachments could easily be added to the widget and draged to the new created sidebar.
The user could then choose this sidebar with a drop-down field in a custom metabox in the post/page admin screens. To display the sidebar, we need also a generic sidebar-template.
Conclusion
From my point of view, Nr.4 should do the trick. Are there any other possibilities? Or does this already exists in a plugin?
Thanks for your reply
There’s a extension called “Widget Logic”. It adds a field to every sidebar widget in admin section where you can add a piece of php code that you can use to archive what you most likely need.
Its a bit cumbersome but works. For example you mention that for multilanguage site, you can still use “is_page” since you can also pass array for the function, like this: is_page(array(94,71,3)) .. With logic operators (and/or/not/xor) combined with you can archive any combination.
I’ve done some custom stuff that deals with things similar to this. Here are some pointers based on what I’ve learned:
_options
table (withupdate_option()
) and read it back on page load. I’ve done this and it’s relatively straightforward.admin_ajax
management options. Most of my users never touch these but they’re invaluable for my own management and debugging.page
but the UI’s already there. If there are only a handful of different types of things you want different, consider adding to your theme or child theming with some extra templates. This will save you a lot of code, debugging and maintenance.dynamic_sidebar()
which you could probably hook into to make existing sidebars act somewhat differently. I would guess rewriting the widget framework with a CPT would make many core devs cringe; nevertheless, if you’ve been working in WP long enough you’ve probably done something like this at some point.get_option()
,get_user_meta()
and$_GET[]
are available at widget rendering. If it’s i8n, you may be better with location-aware content in a custom widget, then a single, contextually-aware, widget in a single sidebar. I’ve seen this done with shortcodes too but as a developer I’m generally happier with the administration determinism of a widget, if the situation allows. Keep in mind that a widget and a shortcode aren’t really that different with shortcode-widgets and widget-shortcodes.WP_Term
tags show up in the CSS classes (the taxonomy structure can be enabled for any post type fairly easily with existing plugins) so consider a system where every possible widget is in the sidebar as configured, but ones that are not applicable per page load are hidden, possibly based on how the post is tagged. This is slightly less compatible but if you have a modern-browser-only allowance in your requirements, that incompatibility is washed away.You asked a thinking question so I gave you a thinking answer. If any of these ideas seem like the direction you want to go, I have a fair amount of code I could draw from for more concrete examples.