Joomla & WordPress Tutorials, Info, Discussion, Tips | GavickPro Blog

Using JavaScript on your theme customization screen

In my recent posts I’ve discussed how you can approach offering theme customization options using PHP – in this and my next few articles I’ll be looking at what we can achieve on the user end of things (that is, the browser) via JavaScript.

Introduction

The JavaScript API is being constantly expanded, including in regards to how one might apply it to a theme customization screen – the last major change introduced in WordPress 4.1 was templates for controls created using the Underscore.js library. In short, some controls are now generated on the basis of a template for the JavaScript language – with this it’s possible to, for example, regenerate the control’s code on the fly, taking into account changes in the value of its options. Before we get to such sophisticated issues, we’ll first need to understand the basics of the API. Let’s look at what each API method is meant to do, along with practical examples of its use on the theme customization screen.

Basics

All collections and API methods of the theme customization screen are included in the wp.customize object. Many developers add a api variable at the beginning to shorten the code:

var api = wp.customize;

If we refer to API from a level of a frame that displays the preview of the theme, we should refer to the parent.wp.customize object.

We may add the code via the customize_preview_init action or customize_controls_enqueue_scripts.

The available controls are contained in the wp.customize.control object, whereas the available settings are contained in the wp.customize object. Sections may be found in the strong>wp.customize.section, and panels in wp.customize.panel. Each of the elements of these objects is an instance of the Values class, which includes several useful methods, such as:

Modifying elements

Elements of the theme customization screen may be modified in many ways- here’s some of the most useful methods:

Additionally, elements in the params firled stores the settings such as label or description:

Another notable field element is container – it includes a handle to an element that stores the control/panel/section. This allows us to modify the elements that we can’t usually modify by changing the parameters and calling the renderContent method.

Preview actions

Through the wp.customize.previewer object we may call actions such as:

Some simple examples

Opening the “colors” section:

wp.customize.section("colors").expand();

Changing the order of the elements:

wp.customize.control("blogname").priority(100);

Transfer control to another section:

wp.customize.control("blogname").section("colors");

Change the name of the control and refresh its structure based on the template:

var ctrl = wp.customize.control("background_color");
ctrl.params.label = "New label";
ctrl.renderContent();
ctrl.ready();

Summary

The current API in JavaScript for the theme customization screen offers some really amazing opportunities that can be used to significantly enrich the theme configuration options.

Share