WordPress设置API使用所选函数保存Bootstrap多选字段



我在为正在创建的插件保存自定义设置页面上的多选字段时遇到问题。看起来甚至在引导之前,多选字段都会从字段中保存1个值,但在选择2时,它不会同时保存两个选项。

我尝试过通常将[]挂在名称的末尾,但这似乎不太可能奏效,因为它已经位于一个数组中,但我不明白为什么在选择了2个值时它不保存。

遵循的代码:

add_action( 'admin_init', 'settings_Init' );
function settings_Init() { 
register_setting( 
'Plugin_Settings', //A settings group name. Should correspond to an allowed option key name.
'Plugin_Settings_Sanitize' //The name of an option to sanitize and save.
);
add_settings_section(
'Plugin_Settings_Section', //$id 
__( 'Settings', 'textdomain' ), //$title
'Settings_Section_Callback', //Function that echos out any content at the top of the section (between heading and fields).
'Plugin_Settings' //The slug-name of the settings page on which to show the section
);
add_settings_field( 
'Select_Field', 
__( 'Directory', 'textdomain' ), 
'Render_Select', 
'Plugin_Settings', 
'Plugin_Settings_Section' 
);
}

function Settings_Section_Callback(  ) { 
echo __( 'This below options must be configured to ensure the plugin works as expected.', 'textdomain' );
}

function Render_Select() { 
$options = get_option( 'Plugin_Settings_Sanitize' );
$products = wc_get_products(array(
'type' => 'course',
'status' => 'published',
'limit' => -1,
));?>

<select class="" name='Plugin_Settings_Sanitize[Select_Field]' multiple>
<?php foreach($products as $product){?>
<option value='<?php echo $product->get_id();?>' <?php selected($options['Select_Field'], $product->get_id());?>>
<?php echo $product->get_title();?>
</option>
<?php }?>
</select>
<?php
}
function options_page(  ) { ?>
<form action='options.php' method='post'>
<h2>Platform</h2>
<?php var_dump($_POST);?>
<?php settings_errors(); ?>
<?php settings_fields( 'Plugin_Settings' ); 
do_settings_sections( 'Plugin_Settings' ); 
submit_button();?>
</form>
<?php }

出于前端目的,渲染选项如下:

<select class="" name="Plugin_Settings_Sanitize[Select_Field]" multiple="">
<option value="17">This is a course test (Copy)</option>
<option value="14" selected="selected">This is a course test</option>
</select>

我已经测试了您的代码。

您必须修复选定的HTML页面。这是Render_Select函数的解释和新版本。

变更日志:

  • Plugin_Settings_Sanitize[Select_Field]更改为Plugin_Settings_Sanitize[],如果您试图在Plugin_Settings_Sanitize中存储其他设置字段,并且希望在Select_Field密钥中保存产品ID,则使用Plugin_Settings_Sanitize[Select_Field][]
  • selected($options['Select_Field'], $product->get_id())更改为selected( in_array( absint( $product->get_id() ), $selected_ids, true ), true )
    ,其中$selected_ids是一个数组(如果选择了产品ID(
    由于要将id存储在数组中,因此selected函数将无法使用该数组。所以我们必须检查所选函数中的CCD_ 12。为此,我们将检查循环中的当前产品id在保存的值中是否可用
    如果它在数组中可用,则为true,否则为false,然后在所选函数中使用该true/false。最重要的是,我将选项值数组映射为absint,这样我们就可以与===进行严格的比较
function Render_Select() {
// Get the options while passing empty array as default value.
$options = get_option( 'Plugin_Settings_Sanitize', array() );
// Check if options is empty and Select_Field key exists and it's data is not empty.
// then converting the type to array using (array), because we want to make sure we have an array
// then we map it with absint function using array_map function so that we have integer
// not string that we are going to use for strict comparison.
$selected_ids = ! empty( $options ) && isset( $options['Select_Field'] ) && ! empty( $options['Select_Field'] ) ? array_map( 'absint', (array) $options['Select_Field'] ) : array();
// Fetch the product array.
$products = wc_get_products(
array(
'type'   => 'course',
'status' => 'published',
'limit'  => -1,
)
);
?>
<select class="" name='Plugin_Settings_Sanitize[Select_Field][]' multiple>
<?php
/**
* Explanation of this code
* selected( in_array( absint( $product->get_id() ), $selected_ids, true ), true )
*
* $is_selected  = in_array( absint( $product->get_id() ), $selected_ids, true ); // check if product id is in selected ids.
* selected( $is_selected, true ); // check if is selected is true, if true write selected="selected" by function.
*/
?>
<?php foreach ( $products as $product ) { ?>
<option value='<?php echo esc_attr( $product->get_id() ); ?>' <?php selected( in_array( absint( $product->get_id() ), $selected_ids, true ), true ); ?>>
<?php echo esc_html( $product->get_title() ); ?>
</option>
<?php } ?>
</select>
<?php
}

最新更新