Note: This is not the popular question about how to create multiple instances of a widget. I have a text widget that I am using to display “business hours”. This text needs to go in multiple sidebars and is likely to change often enough to be a nuisance to change it in multiple places. Is there a way to use the same instance of a text widget on more than one sidebar?
Any alternatives that use tokens or some other kind of pointer would also be a solution.
A possible solution would be to enable shortcodes in the Text Widget.
Then put the same shortcode (i.e.: “
[business_hours]
“) in all text widgets, and all of them will display the same content.It’s even possible to add some Conditionals Tags inside the shortcode definition to show different things according to the context where it is being displayed.
I searched for a hook or filter that might help achieve that but could not find any. There is however
dynamic_sidebar
action but it will be fired after each widget display making its use not practical. So I wrote this helper function which mimics the way WordPress outputs widgets in the sidebar, call this function after the call ofdynamic_sidebar()
where you want a copy of the widget to appear in your the theme:Usage
Place this function in
functions.php
file and then callad_duplicate_widget($widget_id, $sidebar_index, $copy)
right after the call ofdynamic_sidebar()
where you want to copy your widget.$widget_id
is the html id attribute of the widget element.$sidebar_index
can be either the index of the sidebar (number) or the sidebar name (string).$copy
is the custom html id appended to widget id.Example
If you have a text widget of id
'text-5'
that is displayed in the main sidebar of the blog and you want to display it inSidebar Right
sidebar, call this function after thedynamic_sidebar('Sidebar Right')
call, which maybe called fromsidebar.php
orsidebar-right.php
file:This will create a copy of the widget in
Sidebar Right
sidebar, and any edit of the original widget will apply to both widgets.