0

I am making a starting-point Wordpress theme that will be used on dev projects. In this theme, I am automating common plugin installs that are used on every project. I have been able to automate Wordpress Core settings using the update_options() function.

What I now want to do is update options of plugins in the same manner, but I am not having any success.

For example, I use Search Everything as a typical plugin. I have my own search keyword highlighter, so I want to make sure that the plugin setting for search term highlighting is always off (as to not interfere). Here is the code I'm working with:

add_action('admin_init', 'nebula_plugin_force_settings');
function nebula_plugin_force_settings(){
    if ( file_exists(WP_PLUGIN_DIR . '/search-everything') ) {
        //Tried the option slug in the array():
        update_option('se_use_highlight'], false);

        //Tried hard-coding the array and key:
        update_option('se_options["se_use_highlight"]', false);

        //Tried setting the array as a variable:
        $se_options = get_option('se_options');            
        update_option($se_options['se_use_highlight'], false);
    }
}

None of those attempts worked. I feel like I'm getting close because when I var_dump() the array, or echo the get_option() I can see the setting, but I just can not update that setting. Any ideas?

1 Answer 1

1

For reference, you may want to read the update_option documentation. The arguments you are passing are completely unrelated. However, your third technique is the closest, you have to update all the options in the the update_option call, not just the one option you want. This is because of how the plugin is storing it's options as an array rather than individual options.

update_option takes two arguments, the first is the name of the option you want to update (in this case se_options) as a string, the second is the option value, this can be almost anything, it will be converted into a string (serialized) by WP when stored. The Search Everything plugin is sending an array which holds all of it's option settings, you need to do the same.

function nebula_plugin_force_settings() {
    $se_options = get_option( 'se_options' );
    $se_options['se_use_highlight'] = false;
    update_option( 'se_options', $se_options );
}
1
  • Thanks that explanation was exactly what I needed. Appreciate it! Commented Jul 30, 2014 at 18:05

Your Answer

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

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