Support » Plugins » Hacks » Please explain Theme Customization setting default value

  • I was very excited in 3.4 to see the Theme Customization come about, and have read a ton of stuff about it (including the OttoPress posts). I wrote a bunch of customizations for my theme, which will greatly add to the functionality and remove the need for most people to write any CSS or PHP at all. However, when I started testing it out on installs that had never set the specific theme mods before (I’m using theme_mod instead of option), I realized that the defaults I defined in the add_setting statement were not getting used at all. (I know that I could define the default directly in the get_theme_mod( 'modname', mydefaultvalue); but that defeats the purpose of defining the default in the add_setting.

    I just re-read the Theme Customization API documentation in the codex, and noticed something in one of Otto’s comments I hadn’t noticed again. Next to one of the defaults defined in his add_setting example it says “//Default setting/value to save”.

    This makes me think that I’ve been misunderstanding completely how the default works. Should this be passed to get_theme_mod( 'somemod'); if somemod isn’t set in the theme mods, or is it just the default value that will be saved if ome saves the theme customizer without entering a value? (I’m hoping that the default value will be used if somemod isn’t saved in the database, but a default is defined in the add_setting, so the default will work even before they save the Theme Customizer.)

    Do I just need to pass the default during each get_theme_mod call, even though it’s defined in the add_setting?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It may be a good idea, depending on how you use the returned data. By some chance if the add_setting() method were not run for this instance, the lack of a valid setting could cause an issue in your script. Providing a default in get_theme_mod() ensures a valid setting is always returned no matter what has or has not preceded this instance.

    In other words, the ‘add_setting()default gets saved if nothing more specific is specified. Theget_theme_mod()` default is more of an override in case no setting was ever saved, default or otherwise.

    Thread Starter Robert.Vidrine

    (@robertvidrine)

    Thanks bcworkz!
    I had been going on the understanding that the default set during add_setting() was used if get_theme_mod() was not found, but this would be a lot more complex. (The code would have to look for the add_setting() and find if the not found mod name was specified there, then get the default value.)

    My theme update can finally be released!
    (Now that I’m not searching for an error that’s not there…)

    Hi,
    Thanks for clearing that up. I couldn’t find anything else on this topic.

    I added a default in add_setting, just as Robert did. Then I wrote code to display the field. Nothing showed up, and indeed, nothing in the database for that field. So I thought I’d try saving the Customizer settings and see if that did anything. You can’t just Save, because you have to change something in the Customizer settings to allow a Save. I found that unless you change the default value to something else, or change another field, the default value does nothing except sit there looking pretty in the Customizer. 🙂

    Kind of funny design, but at least I get how it works now. I now know that if I want the a default as I envisioned it, best to do it in the get_theme_mod call, and ignore add_settings.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Please explain Theme Customization setting default value’ is closed to new replies.