Wordpress 插件:针对相同设置的多个值
Wordpress Plugin : Multiple values against same setting
我正在编写一个类别折扣插件。在插件设置页面中,我有一个 Category
和 Discount_percentage
字段。我将这些转发如下
function cat_slug_render( ) {
$options = get_option( 'slug-configuration' );
?>
<input type='text' size="50" name='slug-configuration[cat_slug]' value='<?php echo $options['cat_slug']; ?>'>
<?php
}
并将选项定义为
add_settings_field(
'cat_slug', //id
'category Slug', //title
'cat_slug_render', // callable
'slug-configuration', // page
'configuration' // section
);
其他一切都很好。我可以保存设置、检索设置、更新设置等等。
但是,我可以保存 just ONE
值。我的目标是save multiple values using the same fields
.
但是我在这里很困惑我该怎么做。我的最终目标如下
input cat slug
Input discount
Press save
this will insert a New record each time this process is done
非常感谢任何指导或帮助!
您可以在 name
属性中使用 []
在保存期间将其作为数组添加到 [cat_slug]
:
function cat_slug_render( ) {
$options = get_option( 'slug-configuration', array() );
?>
<input type='text' size="50" name='slug-configuration[cat_slug][]' value='<?php echo esc_html( $options['cat_slug'][0] ?? '' ); ?>'>
<?php
}
但是,如果您要保存多个值,则必须决定应将哪个值预加载到字段中。首先?最后?我已经将 [0]
添加到值中,它应该显示第一个,但如果你想要最新的,你可以这样做:
function cat_slug_render( ) {
$options = get_option( 'slug-configuration', array() );
$last_key = array_key_last( $options );
?>
<input type='text' size="50" name='slug-configuration[cat_slug][]' value='<?php echo esc_html( $options['cat_slug'][$last_key] ?? '' ); ?>'>
<?php
}
显然,如果您尝试此操作,请确保更新您的消毒功能,并注意 value
应使用 esc_html()
以及您首选的 isset
方法进行保护(我'如果你想在没有保存任何内容时防止错误,我已经使用了 PHP 7 或更高版本的 ??
。
要显示或使用您保存的值,您可以在 [cat_slug]
数组键上使用 foreach 循环:
$options = get_option( 'slug-configuration', array() )[cat_slug] ?? array();
foreach( $options as $option ) {
// Do stuff.
}
我正在编写一个类别折扣插件。在插件设置页面中,我有一个 Category
和 Discount_percentage
字段。我将这些转发如下
function cat_slug_render( ) {
$options = get_option( 'slug-configuration' );
?>
<input type='text' size="50" name='slug-configuration[cat_slug]' value='<?php echo $options['cat_slug']; ?>'>
<?php
}
并将选项定义为
add_settings_field(
'cat_slug', //id
'category Slug', //title
'cat_slug_render', // callable
'slug-configuration', // page
'configuration' // section
);
其他一切都很好。我可以保存设置、检索设置、更新设置等等。
但是,我可以保存 just ONE
值。我的目标是save multiple values using the same fields
.
但是我在这里很困惑我该怎么做。我的最终目标如下
input cat slug
Input discount
Press save
this will insert a New record each time this process is done
非常感谢任何指导或帮助!
您可以在 name
属性中使用 []
在保存期间将其作为数组添加到 [cat_slug]
:
function cat_slug_render( ) {
$options = get_option( 'slug-configuration', array() );
?>
<input type='text' size="50" name='slug-configuration[cat_slug][]' value='<?php echo esc_html( $options['cat_slug'][0] ?? '' ); ?>'>
<?php
}
但是,如果您要保存多个值,则必须决定应将哪个值预加载到字段中。首先?最后?我已经将 [0]
添加到值中,它应该显示第一个,但如果你想要最新的,你可以这样做:
function cat_slug_render( ) {
$options = get_option( 'slug-configuration', array() );
$last_key = array_key_last( $options );
?>
<input type='text' size="50" name='slug-configuration[cat_slug][]' value='<?php echo esc_html( $options['cat_slug'][$last_key] ?? '' ); ?>'>
<?php
}
显然,如果您尝试此操作,请确保更新您的消毒功能,并注意 value
应使用 esc_html()
以及您首选的 isset
方法进行保护(我'如果你想在没有保存任何内容时防止错误,我已经使用了 PHP 7 或更高版本的 ??
。
要显示或使用您保存的值,您可以在 [cat_slug]
数组键上使用 foreach 循环:
$options = get_option( 'slug-configuration', array() )[cat_slug] ?? array();
foreach( $options as $option ) {
// Do stuff.
}