Support » Plugins » Defaults while using Options Framework

  • Resolved JonathanAlphonso

    (@jonathanalphonso)


    Hi everyone,

    I am having some trouble setting defaults in Options Framework for my wordpress theme. When you install the theme I want my default values, which I’ve set in options.php using the ‘std’ key, to appear on the wordpress site when the theme is installed.

    When I go to theme options, I see the defaults values I set, but they don’t appear until I click the “Save Options” button in the Theme Options tab.

    You can see my dev site before, this is a fresh site with the theme installed and nothing else. I want to make it so that it will show the defaults on a fresh install.


    http://tests.jalphonso.com/wp/

Viewing 15 replies - 1 through 15 (of 18 total)
  • Devin Price

    (@downstairsdev)

    Hi. It’s best if you can pass the default value when you request the option.

    e.g.: of_get_option( $option, $default);

    JonathanAlphonso

    (@jonathanalphonso)

    Wowza, you are totally right, I spent too long looking at the options.php file to notice that 😛

    So now I am trying to get the ‘std’ value in the option arrays to go into the field where you put $default.

    I am having some issues referencing the ‘std’ value tho.

    Any idea on how to make this work? I’ve set all the std values rather nicely.

    of_get_option( $option, std);

    Devin Price

    (@downstairsdev)

    You could write a function to help you get it.

    function of_get_default( $option ) {
         $defaults = optionsframework_options();
         if ( isset( $defaults[$option]['std'] ) ) {
              return $defaults[$option]['std'];
         }
         return false; // default if no std is set
    }

    Then use something like this:

    of_get_option( 'option', of_get_default('option' ) );
    JonathanAlphonso

    (@jonathanalphonso)

    Hey thanks for the function, but in my situation it doesn’t work.

    I found that when you first install the theme, the std are indeed set in the Theme Options interface as they are in the options.php file. But, they are not inserted into the SQL database until I press the “Save Options” button in the Theme Options interface.

    Is there a way to have it push these values to the database upon installation of the theme?

    Devin Price

    (@downstairsdev)

    Why does that solution not work in your case? It allows the ‘std’ default to be pulled directly from the options array without them having to be saved in the database.

    Core developers (like @otto42) recommend that theme options that are not set on activation. It is *possible* tie to the theme activation hook in order to set them, but I’m not sure how that plays out with the theme previewer.

    JonathanAlphonso

    (@jonathanalphonso)

    Ahhh ok, I wasn’t sure where it was being pulled from.

    function of_get_default( $option ) {
         $defaults = optionsframework_options();
         if ( isset( $defaults[$option]['std'] ) ) {
              return $defaults[$option]['std'];
         }
         return false; // default if no std is set
    }

    The function optionsframework_options(); doesn’t exist in the options framework so I had to swap it.

    function of_get_default( $option ) {
         $defaults = get_option('optionsframework');
         if ( isset( $defaults[$option]['std'] ) ) {
              return $defaults[$option]['std'];
         }
         return 'false'; // default if no std is set
    }

    This always returns a false for some reason…

    It might help if you saw the files
    http://jonathanalphonso.com/wp-content/uploads/2014/09/SmallBizWiz-2.zip

    Devin Price

    (@downstairsdev)

    The function “optionsframework_options” is in your theme. That’s how the options are defined and used by the framework. Look in “options.php”.

    You need to pull the array from there, because that’s where the “std” definitions are for each option.

    JonathanAlphonso

    (@jonathanalphonso)

    Ok! Getting closer,

    SO I’ve imported the options.php file in my fuctions.php file, so now I can access the function optionsframework_options

    My code is like this

    require_once('options.php');
    function of_get_default( $option ) {
         $defaults = optionsframework_options();
         if ( isset( $defaults[$option]['std'] ) ) {
              return $defaults[$option]['std'];
         }
         return 'false'; // default if no std is set
    }
    <?php echo of_get_option( 'social_media_editor', of_get_default('social_media_editor')); ?>

    This still returns a ‘false’ though

    Devin Price

    (@downstairsdev)

    Try doing a var_dump of $defaults after it’s called and see what you get.

    var_dump( $defaults );

    Perhaps you could debug why isset( $defaults[$option][‘std’] ) doesn’t have a value.

    JonathanAlphonso

    (@jonathanalphonso)

    Hmm yeah a dump of $defaults is what I would expect…

    Something along the lines of:
    array(17) { [0]=> array(2) { ["name"]=> string(19) "Front-Page Settings" ["type"]=> string(7) "heading" } [1]=> array(6) { ["name"]=> string(29) "Core Competency 1 Text Editor" ["desc"]=> string(184) "This should include a heading 3 and a short bit of text about one of your company's primary offerings. Let visitors know what you are good at and consider adding a Read More... link. " ["id"]=> string(10) "cc1_editor" ["std"]=> string(311)

    Yeah I’ll have to check whats up with that isset…

    Thank you so much for the help Devin!

    JonathanAlphonso

    (@jonathanalphonso)

    OK! I’ve figured some of it out…

    return $defaults[$option][‘std’];

    This will return a false, because $option is will only accept numbers, but it is being set as a string.

    So to get my social_media_editor std value I can write
    return $defaults[8][‘std’];

    I just need to write a script that will find what position my id values are in the array.

    JonathanAlphonso

    (@jonathanalphonso)

    Ok! I figured it out, I am not super great with PHP so it took a bit of time. Here is my function

    //Find which item in the array has a matching id value, then return the std (default) value
    require_once('options.php');
    function of_get_default( $option ) {
        $defaults = optionsframework_options();
        $result = 'default not set';
        for ($i = 0; $i <= count($defaults); $i++) {
            if($defaults[$i]['id'] === $option)
                {
                    $result = $defaults[$i]['std'];
                    break;
                }
        }
         return $result; // default if no std is set
    }

    Not super optimal but it works

    I kind of don’t like how this sort of thing is required though, I use frameworks not to get messy with code 😛

    Devin Price

    (@downstairsdev)

    If you set your options up like this:

    $options['example_text_mini'] = array(
    		'name' => __('Input Text Mini', 'options_check'),
    		'desc' => __('A mini text input field.', 'options_check'),
    		'id' => 'example_text_mini',
    		'std' => 'Default',
    		'class' => 'mini',
    		'type' => 'text');

    Rather than this:

    $options[] = array(
    		'name' => __('Input Text Mini', 'options_check'),
    		'desc' => __('A mini text input field.', 'options_check'),
    		'id' => 'example_text_mini',
    		'std' => 'Default',
    		'class' => 'mini',
    		'type' => 'text');

    My method should work for you, and be a bit cleaner.

    I also mention that in this post about the customizer:

    Options Framework + Theme Customizer

    JonathanAlphonso

    (@jonathanalphonso)

    Ahhhh you are 1000% right Devin!

    I just noticed you are the author of options framework, and I totally should have read your blog posts first before any of this.

    Your method is way better and less hacky so I am for sure going to use it.

    This was the last issue I had with making my WordPress theme so I can release it now 🙂

    A huge thank you to you Devin, for both the framework and the help you’ve provided in this thread.

    Devin Price

    (@downstairsdev)

    Glad to hear you got it all worked out!

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Defaults while using Options Framework’ is closed to new replies.