While using WordPress customizer API I got confused with how the default value parameter is handled by setting. As I realized that setting does not save default value in database and instead one is supposed to use default parameter for get_theme_mod. I personally found it difficult to provide the default value parameter multiple times as part of the get_theme_mod call. So, I wrote a little extra code to save the default values in the database.
Here is an example code for a customizer setting that let the user choose between two blog layouts. The default value for setting id ‘inspiry_post_layout’ is ‘one’ in this example.
To save a default value I created this helper function. Which only saves a default value for a setting in the database, If it does not exist already.
Now, To connect this helper function with the setting declared in the above example and to call it at the right time I wrote the following function and hooked it with the ‘customize_save_after’ action hook.
For Type Option
If you are using the setting of type ‘option’ instead of ‘theme_mod’ then you can replace the helper function code given above with the following code.
I hope the above given code examples will help you with your project.
[…] default values for all my settings and storing them in the database. I came across another interesting article showing you how to do this. Strangely the author uses the customize_save_after hook instead of the […]