• Hi,

    I’m trying to write my very first plugin, based on the Codex example.
    It’s a slider plugin and I would like to add some settings like arrows color, autoplay etc…

    What I’ve done so far is working, but I would like to know how I can add some default values to my option fields. So far, when the plugin is activated, all option fields are empty…

    for example I have this settings field for dots color :

    add_settings_field(
                'slks_dots',
                 __('Dots Color','slick-slider'),
                array( $this, 'slks_dots_callback' ),
                'slick-slider-admin',
                'slks_setting_section_colors'
            );

    and the callback :

    public function slks_dots_callback() {
            printf(
                '<input type="text" id="slks_dots" name="slks_option_name[slks_dots]" value="%s" placeholder="#333333" />',
                isset( $this->options['slks_dots'] ) ? esc_attr( $this->options['slks_dots']) : ''
            );
        }

    How can I have ‘#333333’ as a default value to start with ??

    Next question, the checbox fields.
    I want to have some checboxes returning a true/false value, for example “Enable Auto Play”

    my settings field :

    add_settings_field(
                'slks_autoplay',
                 __('Auto Play','slick-slider'),
                array( $this, 'slks_autoplay_callback' ),
                'slick-slider-admin',
                'slks_setting_section_animation'
            );

    the callback :

    public function slks_autoplay_callback() {
    
    	printf(
                '<input id="%1$s" name="slks_option_name[%1$s]" type="checkbox" %2$s /> <label for="%1$s">Enable Auto Play</label>',
                'slks_autoplay',
                checked (isset( $this->options['slks_autoplay'] ), true, false )
            );
        }

    It’s working but I do not have a default value. when the box is checked, I have a value of 0 returned, when the box is unchecked, no value at all (null)
    and I would like this box to be checked on startup, so the value can be “true” (and “false” if unchecked by the user)

    here is a pastebin of my entire code : http://pastebin.com/U6iDRZkf

    Thanks !

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    ww.wp.xz.cn Admin

    Make a function to combine your default settings with the options list whenever you call get_options.

    So, instead of this:

    $this->options = get_option( 'slks_option_name' );

    Do something like this:

    $defaults = array( 'slks_autoplay' => false, 'other_option' => 123 ); // etc
    $saved_options = get_option( 'slks_option_name' );
    $this->options = array_merge ( $defaults, $saved_options );

    Then call that function instead of calling get_options yourself in each place you need it.

    Thread Starter thomas

    (@beloutte)

    Hello Samuel

    Thanks for your answer ! I’ll try this this afternoon and keep you posted.

    Any idea how the checkbox can return “true/false” instead of “0/null” ?

    Thread Starter thomas

    (@beloutte)

    Hello Samuel,

    Ok I tried your function and it’s working 🙂 except for the checkboxes…
    and also, when I activate the plugin the first time, I have the defaults values, but on the option page input fields are empty, checkboxes uncheked.

    when I change the settings, the checkboxes remain checked even if I uncheked then (but the values are changing…) There is no problem if I delete the default checkboxes values…

    $defaults = array(
            	'slks_fade' => 'false',
            	'slks_speed' => 3000,
    /*
            	'slks_autoplay' => 'on',
            	'slks_autoheight' => 'on',
            	'slks_show_arrows' => 'on',
            	'slks_show_dots' => 'on',
    */
            	'slks_arrows' => '#ffaa00',
            	'slks_dots' => '#003300'
            );

    so, how can I have these checkbox work ? and how can my input fields appear filled when I first visit the option page ?

    here is the whole code : http://pastebin.com/VcvzSe4q

    thanks !!

    Thread Starter thomas

    (@beloutte)

    ok,
    I managed to get it work by changing the code like this:

    $defaults = array(
            	'slks_fade' => 'false',
            	'slks_speed' => 3000,
            	'slks_autoplay' => 'on',
            	'slks_autoheight' => 'on',
            	'slks_show_arrows' => 'on',
            	'slks_show_dots' => 'on',
            	'slks_arrows' => '#444999',
            	'slks_dots' => '#555aaa'
            ); 
    
    $this->options = get_option( 'slks_option_name', $defaults );

    what do you think ?

    Moderator Samuel Wood (Otto)

    (@otto42)

    ww.wp.xz.cn Admin

    You might be using the checked() helper function incorrectly.

    checked (isset( $this->options['slks_autoplay'] ), true, false )

    That’s kinda wrong. What checked() does is to compare the first and second parameters. So using isset doesn’t make sense. If you’re using a checkbox input without a value attribute, then you can just put the true/false there. Like so:

    checked( $this->options['slks_autoplay'], true, false )

    And then have $this->options[‘slks_autoplay’] default to true for example:

    'slks_autoplay' => true,

    However, in your function where you get and save the values, you’ll need to recognize when the value is missing from the input, and set it to false yourself, so that unchecking the box works too.

    Thread Starter thomas

    (@beloutte)

    ok thanks !

    I was sure there was something wrong with the checkboxes 🙂

    what I want is to have values returned : “true” if checked and “false” if uncheked.

    so if I modifiy the code like this, is it correct ?

    '<input id="%1$s" name="slks_option_name[%1$s]" type="checkbox" value="true" %2$s /> <label for="%1$s">'.__("Display Dots","slick-slider").'</label>',
                'slks_show_dots',
                checked (isset( $this->options['slks_show_dots'] ), "true", "false" )

    or should I use your example ? I’ll try it anyway 🙂

    Thread Starter thomas

    (@beloutte)

    I can’t make it work 🙁

    In my $defaults var_dump I have : [“slks_autoplay”] => bool(false)
    (I’ve set ‘slks_autoplay’ => false )

    and I get this in $this->option var_dump : [“slks_autoplay”] => string(2) “on”

    my field is modified like this :

    '<input id="%1$s" name="slks_option_name[%1$s]" type="checkbox" value="false" %2$s /> <label for="%1$s">'.__("Enable Auto Play","slick-slider").'</label>',
     'slks_autoplay',
     checked (isset( $this->options['slks_autoplay'] ), true, false )

    that’s hard ^^

    Moderator Samuel Wood (Otto)

    (@otto42)

    ww.wp.xz.cn Admin

    In php, “on” == true. Get rid of the isset.

    Thread Starter thomas

    (@beloutte)

    ok thanks for your patience !

    so here are my defaults:

    $defaults = array(
        'slks_fade' => 'false',
        'slks_autoplay' => true,
        'slks_autoheight' => true,
        ...
    );

    one of my checkbox:

    printf(
        '<input id="%1$s" name="slks_option_name[%1$s]" type="checkbox" %2$s /> <label for="%1$s">'.__("Enable Auto Play","slick-slider").'</label>',
       'slks_autoplay',
       checked ( $this->options['slks_autoplay'], true, false )
    );

    Everything is working fine on activation; chekboxes are checked with the right default values…
    But if I uncheck just one box and save the settings,I have this debug message:

    Notice: Undefined index: slks_show_arrows

    and most important, all checkboxes become unchecked :/

    what’s wrong ?

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Plugin option page’ is closed to new replies.