1

I have set up my plugin option on admin panel with

   /**
     * Register and add settings
     */
    public function page_init()
    {        
        register_setting(
            'my_option_group', // Option group
            'write_here_options', // Option name
            array( $this, 'sanitize' ) // Sanitize
        );

        add_settings_section(
            'setting_section_id', // ID
            'Set Edit Page', // Title
            array( $this, 'print_section_info' ), // Callback
            'write-here-setting' // Page
        );  

        add_settings_field(
            "pid_num", 
            "Select Page >>", 
            array( $this, 'wh_select_list' ),  
            "write-here-setting", 
            "setting_section_id"
        );

        add_settings_field(
            'num_of_posts', // ID
            'Number of Posts to show', // Title 
            array( $this, 'num_of_posts_callback' ), // Callback
            'write-here-setting', // Page
            'setting_section_id' // Section           
        ); 

    }

So in DB, my plugin setting saved in wp_options table under column name option_name as write_here_options in my case as object.

When people activate the plugin, I want to save default values in the DB for pid_num => 0 and num_of_posts => 10.

How do I make this work??

  • 3
    rather than save default values, just use default values in your code if the options don't exist. – Milo Dec 3 '15 at 22:45
2

Your code will certainly use get_option() to retrieve the values for your options. get_option() accepts a second argument that allows you to specify a default. Use that instead of inserted values into the database unnecessarily.

get_option( $option, $default );

If you are concerned about third party code, there is there is the option_{$option} filter that you should be able to use to preserve your default even then:

116         /**
117          * Filter the value of an existing option.
118          *
119          * The dynamic portion of the hook name, `$option`, refers to the option name.
120          *
121          * @since 1.5.0 As 'option_' . $setting
122          * @since 3.0.0
123          *
124          * @param mixed $value Value of the option. If stored serialized, it will be
125          *                     unserialized prior to being returned.
126          */
127         return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
  • Thank you for the answer. I actually implemented below code to save the default value to DB but I would like to see how's your method will work. I'm not very clear on your answer. How do I save default value without saving them in DB? with your code? – Ohsik Dec 4 '15 at 17:41
  • You don't save the value to the database at all. get_option() will provide the default if nothing is saved to the database. – s_ha_dum Dec 4 '15 at 18:12
  • I think I got it! $default = array( 'pid_num' => '0', 'num_of_posts' => '10' ); $no_exists_value = get_option( 'write_here_options', $default ); var_dump( $no_exists_value ); for my case. However, it does not seem like working when I try to retrieve the default value with $get_nop_setting = get_option('write_here_options'); $post_pp = $get_nop_setting['num_of_posts']; – Ohsik Dec 4 '15 at 18:21
  • @Ohsik Your answer doesn't take into account if a user deactivates the plugin, then turns it back on, all the users customization will be replaced by the defaults. I have posted an answer which deals with this. – Remzi Cavdar Nov 5 '18 at 9:03
0

I added below code to set default value to DB when plugin is activated.

function write_here_activation_actions(){
    do_action( 'wp_writehere_extension_activation' );
}
register_activation_hook( __FILE__, 'write_here_activation_actions' );
// Set default values here
function write_here_default_options(){
    $default = array(
        'pid_num'     => '0',
        'num_of_posts'   => '10'
    );
    update_option( 'write_here_options', $default );
}
add_action( 'wp_writehere_extension_activation', 'write_here_default_options' );
  • 2
    Issue with this is that if you deactivate the plugin, then turn it back on, all the users customization will be replaced by the defaults. – Jeremy Muckel Dec 8 '17 at 21:09
  • @JeremyMuckel I have posted an answer which deals with this particular issue – Remzi Cavdar Nov 5 '18 at 9:02
0

Use add_option instead of update_option. If you use add_option existing options will not be updated and checks are performed to ensure that you aren’t adding a protected WordPress option.

See add_option and update_option at developer.wordpress.org

// Activation
function name_plugin_activation(){
    do_action( 'name_plugin_default_options' );
}
register_activation_hook( __FILE__, 'name_plugin_activation' );


// Set default values here
function name_plugin_default_values(){

    // Form settings
    add_option('name_form_to', 'info@example.com');
    add_option('name_form_subject', 'New');


}
add_action( 'name_plugin_default_options', 'name_plugin_default_values' );

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.