admin管理员组

文章数量:1122832

I have a customizer option that simply creates a dropdown populated with pages from the site. I need this dropdown to have the ability for multiple selections? Any ideas?

$wp_customize->add_control('options_my_dropdown',
array(
    'label' => __('My Label'),
    'description' => esc_html__('My Description'),
    'section' => 'options_section',
    'type' => 'dropdown-pages'
)
);

I have a customizer option that simply creates a dropdown populated with pages from the site. I need this dropdown to have the ability for multiple selections? Any ideas?

$wp_customize->add_control('options_my_dropdown',
array(
    'label' => __('My Label'),
    'description' => esc_html__('My Description'),
    'section' => 'options_section',
    'type' => 'dropdown-pages'
)
);
Share Improve this question edited May 16, 2024 at 13:32 cjbj 15k16 gold badges42 silver badges89 bronze badges asked May 16, 2024 at 13:29 JonJon 3255 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Yes, this is possible, but you will have to really, really want it. It's difficult, because WordPress does not natively support the multiple argument on select inputs in the customizer. You can see this in de the render_content function which outputs the html (just search the function for <select). And even if you would succeed to add the argument, the customizer wouldn't know what to do with the selected items.

So, to get this done, you will have to extend the customizer. In terms of lines of code this is doable. WordPress itself contains multiple extensions to the customizer controls, for instance to add a color picker as an input type. Here's roughly what to do:

  1. Create a new file WP_Customize_Control_Multiselect
  2. Check out the basic content in the color picker. You will need a line public $type = 'multiselect';, a simple constructor like the one in the color picker and a rendering function which you can adapt from the dropdown-pages part of WP_Customize_Control.
  3. Don't forget to include the file in your functions.php and call the control with $wp_customize->add_control (new Customize_Control_Multiselect ...

本文标签: phpWordPress Customizer addcontrol Dropdown of Pages with Multi Select