0

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();
2
  • 1
    What are the contents of the 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 of add_options_page is menu slug, it has nothing to do with what key your options are saved under.
    – Milo
    Feb 3, 2015 at 21:21
  • There's no enough here to work from. Are these within the same class or different? What's in the create_admin_page method?
    – Joey Yax
    Feb 3, 2015 at 21:22

1 Answer 1

1

The add_options_page() adds an option page under Settings menu. It does not add an option. You're registering your settings/options using other functions. Either add_option(), add_settings_field() or register_setting().

Both your options are saved with the same option name, thus overriding each other. You need to save each of them with different option names.

1
  • hi Andrei, I just copied the code. I don't think they are overriding each other I see two records on the database one for each plugin. Yet when I save one plugin the other record's becomes blank. Feb 4, 2015 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.