WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [Plugin: scbFramework] admin page overwrites unreferenced options field (5 posts)

  1. Andrey "Rarst" Savchenko
    Member
    Posted 4 years ago #

    I am making admin page for plugin. Using Smart Archives Reloaded as reference, but simplifying here and there (less objects, omitting localization, etc).

    So I have scbOptions object as global $rds_options variable, setup in init function of plugin.

    One of the option fields is called known_sites, blank array by default, filled by function scheduled with WP Cron.

    I setup my AdminPage class RDS_Settings and put in init

    if ( is_admin() )
    		scbAdminPage::register('RDS_Settings', __FILE__, $rds_options);

    RDS_Settings is not referencing or expected to change known_sites field. But when I hit Save Changes on my settings page known_sites is changed to empty string.

    Am I doing everything right? Is this expected behavior? If it is - should options not expected to be changed by form stored separately or something?

    Thanks in advance.

  2. scribu
    Member
    Posted 4 years ago #

    The default scbAdminPage::form_handler() does indeed update the entire option. I've just made a commit that makes it update only the fields defined in the defaults array.

    So, you have two options:

    1. Make your own form_handler() method in your RDS_Settings class and use $this->options->set() instead of $this->options->update() to update only the fields you want.

    2. Use the development version of scbFramework and remove 'known_sites' from the default fields. You will also need to use the new scb_init() function - see the development version of Smart Archives Reloaded.

  3. Andrey "Rarst" Savchenko
    Member
    Posted 4 years ago #

    Quick and dirty fix that worked for me, I copied form_handler() to my class and changed

    $new_data = array();
    		foreach ( array_keys($this->formdata) as $key )
    			$new_data[$key] = @$_POST[$key];

    to

    $new_data = array();
    		foreach ( array_keys($this->formdata) as $key ) {
    			if ( isset( $_POST[$key] ) )
    				$new_data[$key] = @$_POST[$key];
    			elseif ( isset( $this->options->$key ) )
    				$new_data[$key] = $this->options->$key;
    			else
    				$new_data[$key] = '';
    		}

    Still updates all the fields, but those that aren't in form are filled back with known values.

  4. scribu
    Member
    Posted 4 years ago #

    Careful with that: you won't be able to clear checkboxes (if you have any).

  5. Andrey "Rarst" Savchenko
    Member
    Posted 4 years ago #

    Nope, no checkboxes... As I said - quick and dirty. :) I need to get this thing working and pretty by Sunday.

Topic Closed

This topic has been closed to new replies.

About this Topic