0

I want to set a number of plugin options set to blank if they are not defined so I can avoid the PHP notices.

What is a better way of writing this code?

$options = get_option('plugin_options');

// Add new plugin options defaults here
  if( !isset( $options['plugin_option_1'] ) ) $options['plugin_option_1'] = '';
  if( !isset( $options['plugin_option_2'] ) ) $options['plugin_option_2'] = '';
  if( !isset( $options['plugin_option_3'] ) ) $options['plugin_option_3'] = '';
  if( !isset( $options['plugin_option_4'] ) ) $options['plugin_option_4'] = '';
  if( !isset( $options['plugin_option_5'] ) ) $options['plugin_option_5'] = '';

New options would need to be added in future updates to the plugin.

2

WordPress provides a default method, wp_parse_args, to combine a set of default options and a user-defined set of options. This method uses PHP's native array_merge for arrays, but also works when either of the two option sets is an object or a WordPress options string.

You simply provide an array of default options and an array of set options, and WordPress fills the options that are missing in the options set by their default value.

You can find this code, adjusted for your example, below:

$options = get_option( 'plugin_options' );
$options_default = array(
    'plugin_option_1' => '',
    'plugin_option_2' => '',
    'plugin_option_3' => '',
    'plugin_option_4' => '',
    'plugin_option_5' => ''
);

$options = wp_parse_args( $options, $options_default );
1

Example:

/**
*  assume $options only has three keys defined
*  
*  array(
*      'plugin_option_1',
*      'plugin_option_2',
*      'plugin_option_3',
*  )
*/
$options = get_options('my_options');

$schema = array(
    'plugin_option_1',
    'plugin_option_2',
    'plugin_option_3',
    'plugin_option_4',
    'plugin_option_5',
    'plugin_option_8',
    'plugin_option_9',
    'plugin_option_10',
);

foreach ( $schema as $option ) {

    if ( ! array_key_exists($option, $options) ) {
        $options[$option] = '';
    }

}

Albeit the above example shows you one way to simplify the process, I don't understand why you would be checking for the existence of an undefined key and then assigning it an empty value unless you specifically need to return a sane default?

Why can't you just do...?

if ( !empty($options['some_key']) ) {
    //do my logic...
}
0

Since you have plugin options in array ( with keys ), lets assume your scenario like this:

Your old option is $old = get_option( 'plugin_options' );contain this:

$old = array(
    'plugin_option_1' => '1',
    'plugin_option_2' => '2',
    'plugin_option_3' => '3',
    'plugin_option_4' => '4',
    'plugin_option_5' => '5'
);

Your new option is $new contain this array for updated:

$new = array(
    'plugin_option_2' => '9',
    'plugin_option_5' => '10'
);

Now on plugin update

foreach ( $old as $key => $value )
    $old[ $key ] = ( ! isset( $new[ $key ] ) ) ? '' : $new[ $key ];

update_option( 'plugin_options', $old );

With PHP 7

foreach ( $old as $key => $value )
    $old[ $key ] = ( $new[ $key ]  ) ?? '';

update_option( 'plugin_options', $old );

Result on print_r( $old ):

Array
(
    [plugin_option_1] => 
    [plugin_option_2] => 9
    [plugin_option_3] => 
    [plugin_option_4] => 
    [plugin_option_5] => 10
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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