devinsays/options-framework-plugin

Implement of_set_option

drywall opened this issue · 2 comments

It would be nice if there were a convenience function for setting a particular option via code, outside the context of the options form itself. I'm working on a project where we're retooling the options available and their names, and we'd really like to be able to maintain some backwards compatibility with our old system of options. The easiest way for us to accomplish this would be to load the name of an old option via of_get_option(), run it through a filter/switch statement, and then save it with a different name using an of_set_option()-like call... but I don't see any good way to do that now.

Something like this:

/**
 * Helper for setting specific options
 */
if ( ! function_exists( 'of_set_option' ) ) {
    function of_set_option( $option_name, $option_value ) {
        $config = get_option( 'optionsframework' );

        if ( ! isset( $config['id'] ) ) {
            return false;
        }

        $options = get_option( $config['id'] );

        if ( $options ) {
            $options[$option_name] = $option_value;
            return update_option( $config['id'], $options );
        }

        return false;
    }
}

You're replacing a number of the options names / values on an upgrade? Would something like this work?

function to_run_on_upgrade() {
      $options = get_option('themeoptions', false);
      if ( $options ) {
            $options['newsetting'] = $options['oldsetting'];
            unset( $options['oldsetting'] );
            $options['othersetting'] = 'newvalue';
            update_option( 'themeoptions', $options );
      }
}

If you hadn't explicitly set the option name, then you would have to a little more work to grab that option name to update, e.g.:

$config = get_option( 'optionsframework' );
$optionname = get_option( $config['id'] );
$options = get_option( $optionname, false);

But not too bad.