我正在处理插件设置。首先,我以正常方式保存插件设置,并且完美运行
这是我的旧代码:
的index.php
add_action('admin_init', '_register_settings');
function _register_settings() {
register_setting('_settings_group', '_plugin_slug');
register_setting('_settings_group', '_article_qty');
register_setting('_settings_group', '_search_setting');
register_setting('_settings_group', '_breadcrumbs_setting');
register_setting('_settings_group', '_sidebar_home');
register_setting('_settings_group', '_sidebar_inner');
register_setting('_settings_group', '_comments_setting');
register_setting('_settings_group', '_bgcolor');
}
并在 settings.php
中<?php
if(isset($_GET['settings-updated'])){
echo 'Settings updated successfully';
}
?>
<form method="post" action="options.php">
<?php
settings_fields('_settings_group');
?>
<input type="text" name="_plugin_slug" id="_plugin_slug" value="<?php echo get_option('_plugin_slug'); ?>">
// Same other input fields
<input type="submit" value="<?php _e('Save Changes','foo'); ?>" name="submit" id="submit">
它完全保存了我的值,但现在我更改代码以注册设置并调用其值但不起作用,每当我保存值时它会在我提交表单后显示空白页。
这是我的新代码:
的index.php
add_action('admin_init', '_register_settings');
function _register_settings() {
register_setting( '_settings', '_settings', '_validate_settings' );
}
function kbe_validate_settings( $input ) {
$_clean = array();
$_clean['_plugin_slug'] = isset( $input['_plugin_slug'] ) ? sanitize_title( $input['_plugin_slug'] ) : '';
$_clean['_article_qty'] = intval( $input['_article_qty'] );
$_clean['_search_setting'] = isset( $input['_search_setting'] ) && $input['_search_setting'] ? 1 : 0 ;
$_clean['kbe_breadcrumbs_setting'] = isset( $input['_breadcrumbs_setting'] ) && $input['_breadcrumbs_setting'] ? 1 : 0 ;
$sidebar_positions = array( 0, 1, 2 );
$_clean['_sidebar_home'] = isset( $input['_sidebar_home'] ) && in_array( $input['_sidebar_home'], $sidebar_positions ) ? intval( $input['_sidebar_home'] ) : 0;
$_clean['_sidebar_inner'] = isset( $input['_sidebar_inner'] ) && in_array( $input['_sidebar_inner'], $sidebar_positions ) ? intval( $input['_sidebar_inner'] ) : 0;
$_clean['_comments_setting'] = isset( $input['_comments_setting'] ) && $input['_comments_setting'] ? 1 : 0 ;
$_clean['_bgcolor'] = isset( $input['_bgcolor'] ) ? sanitize_hex_color( $input['_bgcolor'] ) : '';
return $_clean;
}
并在 settings.php
中<?php
$_defaults = array(
'_plugin_slug' => 'Knowledgebase',
'_article_qty' => 5,
'_search_setting' => 0,
'_breadcrumbs_setting' => 0,
'_sidebar_home' => 0,
'_sidebar_inner' => 0,
'_comments_setting' => 0,
'_bgcolor' => ''
);
$_settings = wp_parse_args( get_option( '_settings' ), $_defaults );
?>
<form method="post" action="options.php">
<?php
settings_fields('_settings');
?>
<input type="text" name="_settings[_plugin_slug]" id="_plugin_slug" value="<?php echo esc_attr( $_settings['_plugin_slug'] ); ?>">
// Same other fields
那我在哪里弄错了?
答案 0 :(得分:0)
好的,我遇到了你的问题。在代码中做一些更改,例如。
在index.php中从bgcolor验证中删除此功能
sanitize_hex_color();
记住只删除功能不完整的行,例如
$_clean['_bgcolor'] = isset( $input['_bgcolor'] ) ? sanitize_hex_color( $input['_bgcolor'] ) : '';
更改此代码
$_clean['_bgcolor'] = isset( $input['_bgcolor'] ) ? $input['_bgcolor'] : '';
现在在settings.php中做一些更改。在表单
之前添加此代码if(isset($_settings['update'])){
unset($_settings['update']);
update_option('_settings', $_settings);
}
在提交按钮
之前添加隐藏的输入字段<input type="hidden" name="_settings[update]" value="update" />
希望这会对你有所帮助