I have developed a WordPress plugin that will have multiple settings options. I was able to create a settings page using a configuration menu. The settings page appears well.
When the user changes the boolean setting and then clicks to save the settings, a boolean setting does not seem to be saved, and it appears again as if it was not changed.
I have created a simplified version of the plugin so you can test it and reproduce the situation. The test version of the plugin has the code below.
Is there anything I should change to make the boolean settings be saved and the boolean option check box appears correctly after it is changed and saved by the user?
<?php
/*
* Plugin Name: Test
*/
/*
* test_plugin.php
*
* @(#) $Id: $
*/
class test_wordpress_plugin_use_case_class
{
function RenderSettingsPage()
{
?>
<h2>Test Plugin Settings</h2>
<form action="options.php" method="post">
<?php
settings_fields('test_plugin_options');
do_settings_sections('test_plugin');
?>
<input name="submit" class="button button-primary" type="submit" value="<?php esc_attr_e( 'Save' ); ?>" />
</form>
<?php
}
public function AddSettingsPage()
{
add_options_page('Test plugin page', 'Test Plugin Menu', 'manage_options', 'test_plugin', array($this, 'RenderSettingsPage'));
}
function OptionsValidate($input)
{
$newinput = $input;
return $newinput;
}
function SettingsHeader()
{
echo '<p>Page indexing options</p>';
}
function SettingRemoveFromSearchIndexPagesThatDoNotExist()
{
$options = get_option('test_plugin_settings_remove_from_search_index_pages_that_do_not_exist');
$checked = $options;
echo '<input type="checkbox" name="test_plugin_options[remove_from_search_index_pages_that_do_not_exist]"'.($checked ? ' checked="checked"' : '').' id="test_plugin_settings_remove_from_search_index_pages_that_do_not_exist" />';
}
function RegisterSettings()
{
register_setting('test_plugin_options', 'test_plugin_options', array(
'type'=>'array',
'sanitize_callback'=>array($this, 'OptionsValidate'),
'default'=>array(
'remove_from_search_index_pages_that_do_not_exist'=>true
)
));
register_setting('test_plugin_options', 'test_plugin_settings_remove_from_search_index_pages_that_do_not_exist', array(
'type'=>'boolean',
'default'=>true,
));
add_settings_section('seo_settings', 'Search Engine Optimization Options', array($this, 'SettingsHeader'), 'test_plugin');
add_settings_field('test_plugin_settings_remove_from_search_index_pages_that_do_not_exist', 'Remove from search engines\' index pages that do not exist', array($this, 'SettingRemoveFromSearchIndexPagesThatDoNotExist'), 'test_plugin', 'seo_settings');
}
public function Initialize()
{
add_action('admin_menu', array($this, 'AddSettingsPage'));
add_action('admin_init', array($this, 'RegisterSettings'));
return true;
}
public function Process()
{
return false;
}
public function Finalize($success)
{
return $success;
}
};
$for_the_user = new test_wordpress_plugin_use_case_class;
if($for_the_user->Initialize())
{
$success = $for_the_user->Initialize();
$success = $for_the_user->Finalize($success);
}
Note that Wordpress save checked checkbox setting option as "on" (string), not as boolean value. When the option is unchecked, the option is simply not saved in the options array.
So there are some mistakes and unnecessary things in your code.
Try the following code replacement:
<?php
/*
* Plugin Name: Test
*/
/*
* test_plugin.php
*
* @(#) $Id: $
*/
class test_wordpress_plugin_use_case_class
{
function RenderSettingsPage()
{
?>
<h2>Test Plugin Settings</h2>
<form action="options.php" method="post">
<?php
settings_fields('test_plugin_options');
do_settings_sections('test_plugin');
?>
<input name="submit" class="button button-primary" type="submit" value="<?php esc_attr_e( 'Save' ); ?>" />
</form>
<?php
}
public function AddSettingsPage()
{
add_options_page('Test plugin page', 'Test Plugin Menu', 'manage_options', 'test_plugin', array($this, 'RenderSettingsPage'));
}
function OptionsValidate($input)
{
$newinput = $input;
return $newinput;
}
function SettingsHeader()
{
echo '<p>Page indexing options</p>';
}
function SettingRemoveFromSearchIndexPagesThatDoNotExist()
{
$options = get_option('test_plugin_options');
$option_key = 'remove_from_search_index_pages_that_do_not_exist';
$is_checked = isset($options[$option_key]) && $options[$option_key] === 'on';
echo '<input type="checkbox" name="test_plugin_options['.$option_key.']"'.checked($is_checked, true, false).' id="test_plugin_settings_'.$option_key.'" />';
}
function RegisterSettings()
{
register_setting('test_plugin_options', 'test_plugin_options', array(
'type' => 'array',
'sanitize_callback' => array($this, 'OptionsValidate'),
'default' => array(
'remove_from_search_index_pages_that_do_not_exist' => 'on'
)
));
add_settings_section('seo_settings', 'Search Engine Optimization Options', array($this, 'SettingsHeader'), 'test_plugin');
add_settings_field('test_plugin_settings_remove_from_search_index_pages_that_do_not_exist', 'Remove from search engines\' index pages that do not exist', array($this, 'SettingRemoveFromSearchIndexPagesThatDoNotExist'), 'test_plugin', 'seo_settings');
}
public function Initialize()
{
add_action('admin_menu', array($this, 'AddSettingsPage'));
add_action('admin_init', array($this, 'RegisterSettings'));
return true;
}
public function Process()
{
return false;
}
public function Finalize($success)
{
return $success;
}
};
$for_the_user = new test_wordpress_plugin_use_case_class;
if( $for_the_user->Initialize() )
{
$success = $for_the_user->Initialize();
$success = $for_the_user->Finalize($success);
}
It should better work.