How do I use add_control to offer a list of all pages in the customiser?

To add a Customiser control for selecting a page from a dropdown you just need to set the type argument to dropdown-pages:

$wp_customize->add_control( array(
    'type'    => 'dropdown-pages',
    'section' => 'my_custom_section', 
    'label'   => __( 'Pick a page, bub' ),
) );

This will give you a control that’s a <select> dropdown with a list of pages automatically populated. When saved it will save the post ID of the page, or 0 if none is selected.

The 'allow_addition' argument is an additional argument that can be passed to $wp_customize->add_control() when using dropdown-pages that will allow the user to create a page from the Customiser, just like with the settings in the Home page settings section of the Customiser. You would use this argument like this:

$wp_customize->add_control( array(
    'type'           => 'dropdown-pages',
    'allow_addition' => true,
    'section'        => 'my_custom_section', 
    'label'          => __( 'Pick a page, bub' ),
) );

Leave a Comment