Php wordpress主题选项具有多个值的可重复字段
我正在为我的主题添加一个新的股票选项。问题是数据没有保存。我不知道我在哪里弄乱了我的代码。Php wordpress主题选项具有多个值的可重复字段,php,wordpress,Php,Wordpress,我正在为我的主题添加一个新的股票选项。问题是数据没有保存。我不知道我在哪里弄乱了我的代码。 特殊产品 你在这里遇到了一些问题 您让您的清理过滤器将输入作为字符串进行检查,而实际上它是一个数组 您使用旧方法调用了sanitize_筛选器。看 一些jQuery使用了不推荐使用的速记 在special\u ticker\u text\u callback中,printf的变量数错误。最后一个标记为%5$s,当时只列出了4个 class SpecialProductTicker{ 私人$specia
特殊产品
你在这里遇到了一些问题
您让您的清理过滤器将输入作为字符串进行检查,而实际上它是一个数组
您使用旧方法调用了sanitize_筛选器。看
一些jQuery使用了不推荐使用的速记
在special\u ticker\u text\u callback
中,printf的变量数错误。最后一个标记为%5$s
,当时只列出了4个
class SpecialProductTicker{
私人$special\u产品\u股票期权;
公共函数构造(){
添加操作('admin_menu',数组($this,'special_product_ticker_add_plugin_page');
添加操作('admin_init',数组($this,'special_product_ticker_page_init');
}
公共功能特殊产品股票代码添加插件页面(){
添加子菜单页面(
“exch设置”,
'特殊产品',//页眉
“特殊产品”,//菜单标题
“管理_选项”,//功能
“特殊产品代码”,//菜单\u slug
数组($this,'special\u product\u ticker\u create\u admin\u page')//函数
);
}
公共功能特殊产品股票代码创建管理页面(){
$this->special_product_ticker_options=get_option('special_product_ticker_option_name');?>
特殊产品
谢谢,老兄,你救了我一天,你太棒了。
<?php
class SpecialProductTicker {
private $special_product_ticker_options;
public function __construct() {
add_action( 'admin_menu', array( $this, 'special_product_ticker_add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'special_product_ticker_page_init' ) );
}
public function special_product_ticker_add_plugin_page() {
add_submenu_page(
'exch-settings',
'Special Product', // page_title
'Special Product', // menu_title
'manage_options', // capability
'special-product-ticker', // menu_slug
array( $this, 'special_product_ticker_create_admin_page' ) // function
);
}
public function special_product_ticker_create_admin_page() {
$this->special_product_ticker_options = get_option( 'special_product_ticker_option_name' ); ?>
<div class="wrap" style="background: #fff;padding: 27px 50px;">
<h2>Special Product</h2>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( 'special_product_ticker_option_group' );
do_settings_sections( 'special-product-ticker-admin' );
submit_button();
?>
</form>
</div>
<?php }
public function special_product_ticker_page_init() {
register_setting(
'special_product_ticker_option_group', // option_group
'special_product_ticker_option_name', // option_name
array( $this, 'special_product_ticker_sanitize' ) // sanitize_callback
);
add_settings_section(
'special_product_ticker_setting_section', // id
'Settings', // title
array( $this, 'special_product_ticker_section_info' ), // callback
'special-product-ticker-admin' // page
);
add_settings_field(
'special_ticker_text', // id
'Ticker text & Color', // title
array( $this, 'special_ticker_text_callback' ), // callback
'special-product-ticker-admin', // page
'special_product_ticker_setting_section' // section
);
}
public function special_product_ticker_sanitize($input) {
$sanitary_values = array();
if ( isset( $input['special_ticker_text'] ) ) {
$sanitary_values['special_ticker_text'] = sanitize_text_field( $input['special_ticker_text'] );
}
if ( isset( $input['special_ticker_color'] ) ) {
$sanitary_values['special_ticker_color'] = sanitize_text_field( $input['special_ticker_color'] );
}
return $sanitary_values;
}
public function special_product_ticker_section_info() {
echo'
<style>
.ticker-input-group {
display: inline-flex;
}
.remove {
color: #f00;
cursor: pointer;
}
button.add {
background: #00c008;
border: 0;
padding: 3px 10px 5px;
cursor: pointer;
border-radius: 4px;
color: #fff;
}
</style>
';
}
public function special_ticker_text_callback() {
//get the saved meta as an array
$tickers = $this->special_product_ticker_options;
//var_dump($tickers);
$c = 0;
if(is_array($tickers)){
foreach( $tickers as $key => $ticker ) {
if ( isset( $ticker['special_ticker_text'] ) | isset( $ticker['special_ticker_color'] )) {
printf( '<p>Title <input type="text" name="special_product_ticker_option_name[%1$s][special_ticker_text]" value="%2$s" /> - Color <input class="thebomb-color-picker" type="text" name="special_product_ticker_option_name[%1$s][special_ticker_color]" value="%3$s" /><span class="remove">%5$s</span></p>', $c, $ticker['title'], $ticker['color_code'], __( ' -Remove' ) );
$c++;
}
}
}
if(empty($tickers)){
printf( '<p>Title <input type="text" name="special_product_ticker_option_name[%1$s][special_ticker_text]" /> - Color <input class="thebomb-color-picker" type="text" name="special_product_ticker_option_name[%1$s][special_ticker_color]" /><span class="remove">%2$s</span></p>', $c, __( ' -Remove' ) );
}
?>
<span id="FieldHere"></span>
<button class="add"><?php _e(' + Add New'); ?></button>
<script>
var $ =jQuery.noConflict();
$(document).ready(function() {
var count = <?php echo $c; ?>;
$(".add").click(function() {
count++;
$('#FieldHere').append('<p>Title <input type="text" name="special_product_ticker_option_name['+count+'][special_ticker_text]" value="" /> - Color <input class="thebomb-color-picker" type="text" name="polls['+count+'][special_ticker_color]" value="" /><span class="remove"> -Remove</span></p>' );
// turn the cloned field into a colorpicker
$('.thebomb-color-picker').wpColorPicker();
return false;
});
$(".remove").live('click', function() {
$(this).parent().remove();
});
//Color picker
$('.thebomb-color-picker').wpColorPicker();
});
</script>
<?php
}
}
if ( is_admin() )
$special_product_ticker = new SpecialProductTicker();
class SpecialProductTicker {
private $special_product_ticker_options;
public function __construct() {
add_action( 'admin_menu', array( $this, 'special_product_ticker_add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'special_product_ticker_page_init' ) );
}
public function special_product_ticker_add_plugin_page() {
add_submenu_page(
'exch-settings',
'Special Product', // page_title
'Special Product', // menu_title
'manage_options', // capability
'special-product-ticker', // menu_slug
array( $this, 'special_product_ticker_create_admin_page' ) // function
);
}
public function special_product_ticker_create_admin_page() {
$this->special_product_ticker_options = get_option( 'special_product_ticker_option_name' ); ?>
<div class="wrap" style="background: #fff;padding: 27px 50px;">
<h2>Special Product</h2>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( 'special_product_ticker_option_group' );
do_settings_sections( 'special-product-ticker-admin' );
submit_button();
?>
</form>
</div>
<?php }
public function special_product_ticker_page_init() {
register_setting(
'special_product_ticker_option_group', // option_group
'special_product_ticker_option_name', // option_name
array(
'type' => 'array',
'sanitize_callback' => array($this, 'special_product_ticker_sanitize'),
'default' => NULL
) // sanitize_callback
);
add_settings_section(
'special_product_ticker_setting_section', // id
'Settings', // title
array( $this, 'special_product_ticker_section_info' ), // callback
'special-product-ticker-admin' // page
);
add_settings_field(
'special_ticker_text', // id
'Ticker text & Color', // title
array( $this, 'special_ticker_text_callback' ), // callback
'special-product-ticker-admin', // page
'special_product_ticker_setting_section' // section,
);
}
public function special_product_ticker_sanitize($input_array) {
$sanitary_values = array();
foreach ($input_array as $key => $input){
if (isset($input['special_ticker_text'])) {
$sanitary_values[$key]['special_ticker_text'] = sanitize_text_field($input['special_ticker_text']);
}
if (isset($input['special_ticker_color'])) {
$sanitary_values[$key]['special_ticker_color'] = sanitize_text_field($input['special_ticker_color']);
}
}
return $sanitary_values;
}
public function special_product_ticker_section_info() {
echo'
<style>
.ticker-input-group {
display: inline-flex;
}
.remove {
color: #f00;
cursor: pointer;
}
button.add {
background: #00c008;
border: 0;
padding: 3px 10px 5px;
cursor: pointer;
border-radius: 4px;
color: #fff;
}
</style>
';
}
public function special_ticker_text_callback() {
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script('wp-color-picker');
//get the saved meta as an array
$tickers = get_option('special_product_ticker_option_name');
$c = 0;
if(is_array($tickers)){
foreach( $tickers as $key => $ticker ) {
if ( isset( $ticker['special_ticker_text'] ) | isset( $ticker['special_ticker_color'] )) {
printf( '<p>Title <input type="text" name="special_product_ticker_option_name[%1$s][special_ticker_text]" value="%2$s" /> - Color <input class="thebomb-color-picker" type="text" name="special_product_ticker_option_name[%1$s][special_ticker_color]" value="%3$s" /><span class="remove">%4$s</span></p>', $c, $ticker['special_ticker_text'], $ticker['special_ticker_color'], __( ' -Remove' ) );
$c++;
}
}
}
if(empty($tickers)){
printf( '<p>Title <input type="text" name="special_product_ticker_option_name[%1$s][special_ticker_text]" /> - Color <input class="thebomb-color-picker" type="text" name="special_product_ticker_option_name[%1$s][special_ticker_color]" /><span class="remove">%2$s</span></p>', $c, __( ' -Remove' ) );
}
?>
<span id="FieldHere"></span>
<a class="add button"><?php _e(' + Add New'); ?></a>
<script>
var $ =jQuery.noConflict();
$(function() {
let count = <?php echo $c; ?>;
$(".add").on('click', function() {
count++;
$('#FieldHere').append('<p>Title <input type="text" name="special_product_ticker_option_name['+count+'][special_ticker_text]" value="" /> - Color <input class="thebomb-color-picker" type="text" name="special_product_ticker_option_name['+count+'][special_ticker_color]" value="" /><span class="remove"> -Remove</span></p>' );
// turn the cloned field into a colorpicker
$('.thebomb-color-picker').wpColorPicker();
return false;
});
$(".remove").on('click', function() {
$(this).parent().remove();
});
//Color picker
$('.thebomb-color-picker').wpColorPicker();
});
</script>
<?php
}
}
if ( is_admin() )
$special_product_ticker = new SpecialProductTicker();