Quantcast
Channel: WordPress – Befive.Info
Viewing all articles
Browse latest Browse all 7

Getting get_theme_mod working in WordPress Customizer Live Preview

0
0

Yesterday I came across a problem that get_theme_mod was not working in the WordPress Customizer Live Preview. So to speak, it is yet another “It does not work” problem.

Here is the snippet of summary.

View the code on Gist.

Depending on how you want to use the theme mod options, you want to get the theme mod option values before your WordPress template files are loaded. For example, if you want to load all the option values at once even before WordPress start outputting html, you might try to get the values in functions.php. Now you have a trouble that the WordPress Customizer’s Live Preview does not show with the changes in the settings field reflected in the preview pane. This is because the Customizer uses filter hook at some point to emulate how it would be with the changed settings in review. A WordPress developer, Otto, wrote in his blog post to introduce the WordPress Customizer:

You already have a theme options page, right? So somehow, you’re saving those options. And in the theme, it’s reading those options using get_theme_mod or get_option, right? The way the theme customizer works is that it intercepts that call using a filter, changes the option for the previewer case only, and then passes the new option along to the theme. So the theme has no idea that the value it’s getting isn’t in the database, but one the user just selected.

Determining the right action hook is a matter of debate. So I made a experiment theme to show at which hook the theme mod values can be retrieved in the WordPress Customizer Live Preview. The WordPress theme is available at gitHub for you to try. You need to install and activate Kirki plugin.

customizer_live_preview_test

Once you activate the theme, go to the front page and click on the Customize link in the admin bar. Then proceed to “Test Customizer Live Preview Option Value” panel, then to “Test Section” section. Try changing the value in the pull down menu to see how the setting item is reflected.

The post Getting get_theme_mod working in WordPress Customizer Live Preview appeared first on Befive.Info.


Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images