I’m on a nightly quest to build a custom single-use widget.
As soon as one instance of it has been added to a sidebar panel in the Customizer, its control on the Available Widgets panel should be displayed as disabled (or alternatively, disappear entirely).
This is how it would look (note the visually “disabled” widget on the right):
The custom Widget is registered and all, but I’m stuck at the single-use requirement.
- Scale is not a problem. It’s ok to assume only 1 registered sidebar. The approach can be to limit the use of the widget to either 1 per sidebar, or to 1 per all registered sidebars—both would be equally fine for now.
- The Widgets page on the back-end is not a problem. This is not required to work equally well on the Widgets page under Appearance on the back-end. It only has to work in the Customizer.
- About 250 years ago, all widgets used to be “single-use”. Does anyone know of a legit way to bring those times back and make a custom widget be used only 1x through the Widget API?
What I tried so far
- Dug through a fair chunk of core files.
- Read this, and this, but neither one seems practical.
- Tinkered with jQuery events
widget-synced, but miss an event for “widget deleted”.
Thanks tons in advance!
Update: Haven’t put my proof of concept on a public repo yet, would will do, of course.
I wrapped the solution kindly shared by kraftner below in a proof of concept plugin on GitHub.