WordPress Theme Customizer Interaction

The first part of the WordPress Theme Customizer series discussed the need to load the $wp_customize object, which is an instance of the WP_Customize_Manager class, in order to interact with Theme Customizer. This can be done by using the customize_register action hook and placing the code in the theme’s functions.php or a file that is included from it.

Once the $wp_customize object is loaded, any of its methods can be used to add, get or remove settings, controls and sections in it. For example, to remove a section, control or setting, all that is needed is its ID. To add a section, control or setting, more parameters are required.

An example of adding a new section, setting and control to Theme Customizer is provided. The section is labeled “Layout” and allows users to edit their theme’s layout. The setting stores the theme’s layout and the control is a radio button with two options – sidebar on the left and sidebar on the right.

After adding these code snippets to the function hooked into customize_register action hook, Theme Customizer has been customized. Users can now store this setting and its value can be retrieved using get_option function. The value can then be added to the array of existing body classes using the body_class filter hook. By using these classes in the theme’s style.css file, two different layouts can be created.

Part Three of this series will focus on automating the entire process and creating a Theme Customizer Boilerplate that can be dropped into a theme and used right away.

Stay in Touch

spot_img

Related Articles