我创建了两个插件,当我保存一个设置时,它会完全删除另一个的设置,反之亦然。
我真的不明白问题可能是什么,因为它们不共享相同的常规配置:一个插件选项保存在pl\\u popup\\u options中,另一个保存在pl\\u analytics\\u options中。然而,当我更新第一个时,它会从数据库中删除另一个的值。
有人面临过类似的问题吗?这可能是什么原因?
插件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();
插件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();