I created two plugins, when I save one settings it completely erases the settings from the other one, and it also happens the other way round.
I really don't understand what the issue might be about since they don't share the same general configuration: one plugin options are saved in pl_popup_options and the other in pl_analytics_options. Yet when I update the first one, it erases the value of the other from the database.
Anybody faced a similar issue? What could be the reason for this?
Plugin 1
class PLAnalyticsSettings
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'Pranalog Analytics',
'manage_options',
'pl_analytics_options',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'pl_analytics' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Pranalog Analytics Settings</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'my_option_group' );
do_settings_sections( 'pl_analytics_options' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Ingresa el código debajo y se añadirá en el header a computadoras que no hayan registrado un admin:';
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'my_option_group', // Option group
'pl_analytics', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'general_settings', // ID
'Pranalog Analytics', // Title
array( $this, 'print_section_info' ), // Callback
'pl_analytics_options' // Page
);
add_settings_field(
'code', // ID
'Código', // Title
array( $this, 'code_callback' ), // Callback
'pl_analytics_options', // Page
'general_settings' // Section ID
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
// TODO: Sanitize
return $input;
}
/**
* Get the settings option array and print one of its values
*/
public function code_callback()
{
printf('<textarea type="text" id="code" name="pl_analytics[code]" style="min-width: 80%%; max-width: 80%%; min-height: 250px; max-height: 400px; font-size: 12px;">%s</textarea>',
isset($this->options['code']) ? esc_attr( $this->options['code']) : ''
);
}
}
if(is_admin())
$pla_settings_page = new PLAnalyticsSettings();
Plugin 2
class PLPopupSettings
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'Pranalog Popup',
'manage_options',
'pl_popup_options',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'pl_popup' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Pranalog Popup Settings</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'my_option_group' );
do_settings_sections( 'pl_popup_options' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Pranalog Popup te permite mostrar un popup personalizado en todo tu sitio';
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'my_option_group', // Option group
'pl_popup', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'general_settings', // ID
'Pranalog Popup', // Title
array( $this, 'print_section_info' ), // Callback
'pl_popup_options' // Page
);
add_settings_field(
'enabled', // ID
'Activado', // Title
array( $this, 'enabled_callback' ), // Callback
'pl_popup_options', // Page
'general_settings' // Section ID
);
add_settings_field(
'content', // ID
'Contenido del Popup', // Title
array( $this, 'content_callback' ), // Callback
'pl_popup_options', // Page
'general_settings' // Section ID
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
// TODO: Sanitize
return $input;
}
/**
* Get the settings option array and print one of its values
*/
public function enabled_callback()
{
// echo the field
printf(
'<input type="checkbox" id="enabled" value="checked" name="pl_popup[enabled]" %s/>',
isset( $this->options['enabled'] ) ? esc_attr( $this->options['enabled']) : ''
);
}
public function content_callback()
{
printf(
'<textarea type="text" id="content" name="pl_popup[content]" style="min-width: 80%%; max-width: 80%%; min-height: 250px; max-height: 400px; font-size: 12px;">%s</textarea>',
isset($this->options['content']) ? esc_attr( $this->options['content']) : ''
);
}
}
if(is_admin())
$plp_settings_page = new PLPopupSettings();
create_admin_page
function? Both of your options pages reference that function, so presumably you're not differentiating between the two option pages within. The 4th argument ofadd_options_page
is menu slug, it has nothing to do with what key your options are saved under.