I am working on a plugin settings. First I save plugin settings in normal way and it works perfectly
Here is my old code:
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');
}
and in 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">
It save my values perfectly but now i change the code to register settings and call its values but its not working, whenever i save values it shows me a blank page after i submit the form.
Here is my new code:
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;
}
and in 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
So where am I making a mistake?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire