每次我保存插件选项时,它都会删除另一个插件选项

时间:2015-02-03 作者:Lisandro Vaccaro

我创建了两个插件,当我保存一个设置时,它会完全删除另一个的设置,反之亦然。

我真的不明白问题可能是什么,因为它们不共享相同的常规配置:一个插件选项保存在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();

1 个回复
SO网友:tao

这个add_options_page() adds an option page 在设置菜单下。信息技术does not add an option. 您正在使用其他功能注册设置/选项。任何一个add_option(), add_settings_field()register_setting().

您的两个选项都使用相同的option name, 从而相互超越。您需要使用不同的选项名称保存每个选项。

结束

相关推荐

如何将值添加到作为数组的wp_Options选项?

向中注册的期权添加期权值的最佳方法是什么wp_options 那是一个数组?对于phpmyadmin,我看到option\\u value字段是a:9:{i:3;s:15:\"value1\";i:6;s:5:\"value2\";i:7;s:21:\"value3\";i:8;s:15:\"value4\";i:9;s:7:\"value5\";i:10;s:4:\"value6\";i:11;s:5:\"value7\";i:12;s:8:\"value8\";i:13;s:7:\"value9\";