How do I create a widget that only allows a single instance of a widget to be added through the WordPress interface?
The default behaviour of widgets allows multiple instances to be added.
I am using the Example code from http://codex.wordpress.org/Widgets_API
I have seen a similar question where the answer was to use the old widget code, but I would like to continue to use that more modern code if possible.
I have seen plugins such as Twitter for WordPress that give exact control over the number of instances allowed.
Thanks,
matt
I know this is a super old question, but searching for solutions 8 years later didn’t yield many results aside from this unresolved question (and a few other vague answers). Nothing I found on this site worked for me so if others arrive on this page looking for a way to limit a widget-area to single-use from a specific widget, I’ve provided the code that worked for me below. This solution is based on an article posted by Tom McFarlin. It makes use of jQuery’s sortable function to check the widgets according against criteria every time it tries to update. I made some of my own tweaks to better suit my needs.
Please note: You’ll need to replace
widget_area_id
with your own ID for the specific WIDGET AREA as well aswidget_id
with your own ID for the actual WIDGET. Place this code in anadmin.js
file and enqueue it on the backend.Switch after the 1st instance and abort
In the following example you can find a
static
variable that’s not set on the first instance. If the widget gets called the 1st time, we set it to true. On the 2nd run, we abort and justreturn
without actually serving anything to the user.Code taken from the related Widgets API Codex article
Handling the global widgets array
Another option might be to check the global array containing all registered widgets:
The output of the associative array contains the name as key, appended by
-2
(nr. is incremented). You could search through the array on theinit
oradmin_init
hook and simply unset if you have found a second instance. Maybe something like the following:Edit: Doh, just saw that you didn’t want to use to the old code. Oh well, I’ll leave it just in case it helps someone.
WP added multi widgets in 2.8. So, I tracked down a tutorial that covered how to make a widget in WP 2.7. Here’s the code:
I replaced the calls to
register_sidebar_widget
andregister_widget_control
withwp_register_sidebar_widget
andwp_register_widget_control
respectively, to fix warnings about using deprecated functions. Otherwise, this is a straight copy/paste from the tutorial I linked to.Hereâs a proof of concept for a single-use widget derived from this answer:
https://github.com/glueckpress/single-use-widget
Limitation: Only works in the Customizer, not on Widget admin page.
You don’t. It’s too difficult to be worth it. It’s how the JS editor thing works.
BUT: Create a Widget with no settings and allow setting it up on a different page. Like this:
Use add_options_page to create the SettingsPage page for Widget management. And use get_option() to retrieve the settings for Widget Output.
Regards.
PS: If you need a full example, let me know.