1

I have created a new plugin setting in an existing plugin that needs to be checked by default when users update to the new version because a lot of the original features are now activated by this checkbox.

This is the setting from my settings page:

<td>
    <?php printf(
        '<input id="bodhi_svgs_settings[advanced_mode]" name="bodhi_svgs_settings[advanced_mode]" type="checkbox" %2$s />', 'bodhi_svgs_settings_advanced_mode', checked( isset( $bodhi_svgs_options['advanced_mode'] ), true, false ) ); ?>
    <?php _e( 'Yes', 'svg-support' ); ?><br /><small class="description"><?php _e(' You don\'t need to enable this to simply use SVG files as images. Enabling this will trigger advanced options and SVG functionality such as inline rendering.', 'svg-support' ); ?></small>
</td>

I've looked at a number of solutions and can't seem to get it to work properly. I tried checked( isset( $bodhi_svgs_options['advanced_mode'] ), true ) but that outputs checked="checked" to the page. I can't figure out how to do it without rewriting the entire options to work a different way.

1 Answer 1

3

If you check the checkbox and save the plugin options it will add option to the {prefix}_options table in the database. To make a checkbox checked you need to update_option('option_id', 'value' ) for that checkbox on plugin activation. to do that you need to register plugin activation hook. Place the below code in the main plugin file.

register_activation_hook( __FILE__, 'am_plugin_activate' );
function am_plugin_activate() {
    // plugin activation code here...
    update_option('checkbox_id', true );
}

this will make the checkbox checked. to make the checkbox checked on plugin update you need to hook a function on upgrader_process_complete action hook. e.g;

add_action( 'upgrader_process_complete', 'am_plugin_upgrate',10, 2);
function am_plugin_upgrate( $upgrader_object, $options ) {
    // plugin update code here....
}

for reference please take a look at upgrader_process_complete, Discussion Plugin Activation Hooks and Plugin activation hook discussion

8
  • Forgive my ignorance, but I'm not sure what to set as 'checkbox_id'. I tried 'bodhi_svgs_settings[advanced_mode]', 'advanced_mode', $bodhi_svgs_options['advanced_mode']. I also tried putting global $bodhi_svgs_options; inside the function. I'm sure I'm missing something simple! Your solution looks awesome though, thank you.
    – Benbodhi
    Commented Aug 18, 2016 at 22:56
  • checkbox_id is the option name you wish to update. assuming your checkbox has it's own id when you registered your setting page, that's what goes there. If you save all your options as an array, then you need to modify the array at the index which contains your option value. then use update_option()
    – bynicolas
    Commented Aug 18, 2016 at 23:05
  • that's where I'm confused, I thought my option name was advanced_mode...
    – Benbodhi
    Commented Aug 18, 2016 at 23:11
  • rereading your question. your option name in wp_options table seem to be bodhi_svgs_settings and your checkbox id seems to be advanced_mode. Then you would use update_options( 'bodhi_svgs_settings', array( 'advanced_mode' => true ) );
    – bynicolas
    Commented Aug 18, 2016 at 23:13
  • awesome, that worked with the slight change to update_option rather than update_options. But now all of the other existing settings are cleared.
    – Benbodhi
    Commented Aug 18, 2016 at 23:17

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.