Settings API – how to update multiple options manually?

  1. Fetch current options, and be sure to return an array if empty:

    $current_options = get_option( 'option_name', array() );
    
  2. Ensure desired options are in an array:

    $desired_options = array( /* whatever you need goes here */ );
    
  3. Merge them, using [wp_parse_args()]1:

    $merged_options = wp_parse_args( $current_options, $desired_options );
    
  4. Update

    update_option( 'option_name', $merged_options );
    

Of course, the trick for you is ensuring that you’re dealing with an array with your desired options. Maybe do something like so:

$desired_options = array( $_POST['import'] );

(P.S. please do some sort of sanitization. $_POST data are inherently untrustworthy.)

Edit

I have around 30 options, inputs, booleans, textareas. They’re all
stored under XX_theme_settings and one of them is called
existing_field

Just follow the process laid out above:

  1. Fetch current options, and be sure to return an array if empty:

    $current_options = get_option( 'XX_theme_settings', array() );
    
  2. Ensure desired options are in an array:

    $desired_options = array( 'existing_field' => 'your string here' );
    
  3. Merge them, using [wp_parse_args()]1:

    $merged_options = wp_parse_args( $current_options, $desired_options );
    
  4. Update

    update_option( 'XX_theme_settings', $merged_options );
    

Leave a Comment