Default value in get_theme_mod function
Is there a way to get the default values from kirki when making a call to get_theme_mod? Right now, I have to set the default in kirki, and then I set the same default in my get_theme_mod call, which seems like a duplication of effort. Just thought I'd ask to make sure I wasn't missing something.
We will be introducing a new get_kirki_option soon that will do that.
For the time being we're using the default WordPress functions and unfortunately that's the downside of it...
It will be improved though soon. :)
@aristath -
Thanks! I think kirki is really great, and I appreciate all your work on it.
Fixed in https://github.com/aristath/kirki/commit/78e638e8acbeabc1d12305ca85904007c4015518
Not yet merged 'cause I'm still testing that branch, but it will be included in the next version. :smile:
Update:
I tried this and ended up spending a lot of time staring at my screen without understanding why this wouldn't work. For now I just commented-out this function so it won't be available.
If anyone has any ideas then please let me know... This is the code we currently have: https://github.com/aristath/kirki/blob/12f8141d838f2178306a836e2fbf59614eaff8bf/kirki.php#L178-L223
For the time being you will simply have to define a default value when using get_theme_mod()
Also looking forward into this feature.
@aristath Do you have any ETA on this feature? Just realised this breaks my sites when deploying because processing the output values in other functions.
Actually that's the default WordPress functionality... get_theme_mod needs a default value.
I'm trying to find a workaround though I don't currently have an ETA
Sure, i get that. No stress, going with a workaround for the time being.
Hey there! It took me 31 commits and I had to re-structure the whole plugin, but I got this working. :+1:
You can now use kirki_get_option( 'my_option' ); instead of the get_theme_mod function and it will automatically get the default value.
However, you may want to add the following in the theme you're building:
if ( ! function_exists( 'kirki_get_option' ) ) :
function kirki_get_option( $option ) {
get_theme_mod( $option, '' );
}
endif;
This way if the plugin is not installed you won't get the right default value for unset controls, but at least the universe will not collapse on its self due to a fatal error.
Going to test this now, i'll let you know if something goes wrong. Thanks for the all the work.
I tried kirki_get_option, but got a notice
kirki_get_option is deprecated since version 1.0.0! Use get_theme_mod or get_option instead
Is it impossible to get default value in the latest version?
@kamiyeye hey there!
Yes, that function has been deprecated since version 1.0
Both get_theme_mod and get_option have the default value as a 2nd argument so that's what you should use.
Internally we still have an implementation of this which is Kirki_Values::get_value( $config_id, $setting ); but that is only used internally in the plugin and should not be used in a theme or plugin because it's not meant for public use as it may change or even be removed at some point.
If you want you can write your own custom function and use it in your theme using https://github.com/aristath/kirki/blob/v3.0.22/core/class-kirki-values.php as an example and hardcoding your config-id there, tweaking it to your liking and use-case.
However, instead of doing all that I'd recommend you use the default WordPress functions. Less overhead this way 👍
Any update on adding this utility method?
Any update on adding this utility method?
Which utility method? If you mean adding a method that will allow you to skip defining the default value when you're getting a theme-mod then no, this will not be implemented.
It was part of earlier versions of the plugin years ago, but since then we're trying to instead educate theme authors and teach them to do the right thing: Use the 2nd argument in the get_theme_mod() function to get the default value if a saved one does not exist. 😄
I actually solved it doing a init setup if its detected that some reference value isnt yet loaded into the options, so it only runs once.
if(empty(get_theme_mod("some variable") )){ error_log("init"); foreach (Kirki::$fields as $field ) { if(isset($field["default"]) && !isset($redux_demo[$field["settings"]])){ set_theme_mod($field["settings"],$field["default"]); } }
}