1

What is a proper way to add a list of pages (type: dropdown-pages) using Customizer JS API?

Currently, I have this code but it does not display a control:

api.control.add( new api.Control( 'custom-control', {
  type: 'dropdown-pages',
  section: 'custom-section',
  setting: new wp.customize.Setting( 'custom-control', '0' ),
  label: 'Select Page'
} ) );

If I change the type to, for example, text then the control is shown in the section.

3

You're right, that doesn't work… yet. We didn't add support for the dropdown-pages control in #30738 because we wanted to leverage the REST API for this control to fetch the pages. So in 4.9 this is the only base control that requires server-side rendering in core. However, in 4.9 it is easy to provide our own implementation of a content template for the dropdown-pages control.

I've written a standalone example plugin which includes control template for the dropdown-pages control type. With that template included, you can create controls in the same way you are doing here. Follow #42272 for upcoming core implementation.

Alternatively, you may want to consider the Customize Object Selector plugin, as it provides a control which uses Select2 to provide a searchable interface for selecting one or more pages. See example usage in the Customize Posts plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.