How can I set and get the values of a multiple select with the Wordpress settings API for a theme options page?

我只是一个虾纸丫 提交于 2019-12-10 21:12:37

问题


I want to use a 'multiple select field' for a field in the theme option page using settings API.

I tried with the below code but unable to save all the selected values, it only saves last one selected.

add_action('admin_menu', 'create_theme_options_page');

function create_theme_options_page() {  
add_options_page('Theme Options', 'Theme Options', 'administrator', 'inc/site-options.php', 'build_options_page');
}

add_action('admin_init', 'register_and_build_fields');

function register_and_build_fields() {   
register_setting('plugin_options', 'plugin_options', 'validate_setting');
add_settings_field('clusters', 'Choose Clusters to show:', 'cluster_setting', __FILE__, 'site_section');
} 

function cluster_setting() {  
$options = get_option('plugin_options');

echo "<select name='plugin_options[clusters]' multiple='multiple'>
<option value='location'>Location</option>
<option value='role'>Role</option>
<option value='functional'>Functional Area</option>
<option value='industry'>Industry</option>
</select>"
}

How can I select multiple values from the select element and save them.

Any help will be appreciated.


回答1:


try using

<select name='plugin_options[clusters][]' multiple='multiple'>

instead. This will ensure that the clusters value is saved as an array instead of a string



来源:https://stackoverflow.com/questions/17987233/how-can-i-set-and-get-the-values-of-a-multiple-select-with-the-wordpress-setting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!