Theme Customizer – setting in WordPress

To get started, to understand what Theme Customizer is, watch the video from youtube, think, much will become clear. After watching the video, you probably won't need a plugin OptionTree which I spoke about before.

I think after watching you will have a lot of ideas on how you can use this useful panel, and here is how it works, let's take a look below.

Existing Options

The first thing I think is important to consider is the existing theme customization methods., as many authors write them 3 the option, let's believe them and try to learn how to use these methods.

  • There are mods, which are supported to work with the theme, You can control these mods using the functions set_theme_mod and get_theme_mod. These features are supported Settings API WordPress and work great.
  • Individual settings for each theme. Not the best way to create topics, but it also has a place to be and is also supported in Theme Customizer.
  • Serialized Settings – In fact, you store the settings in an array, and then save this array using set_option or get_option, as one row in the database. This is the most optimal method and we will consider it in this article..

We briefly mentioned the other two methods and if we need them, then we will consider them.
Firstly in Theme Customizer left side is divided into sections and it is worth paying attention! Everything works through a special action (a pig) customize_register.

The function receives as a parameter $wp_customize this is the main object. We will work with it when adding sections, controls and the like.

Sections in Theme Customizer

So, first, what should be done, is to add a section. Here is one way to do it:

While there are no controls in the section and they are not configured, the new section will not be displayed, we'll talk about controls a bit later., and now let's look at the code that we described above.
themename_color_scheme – the first parameter is the unique id to display, we will need this identifier later when we place controls in the section.

The second parameter is an array. This array has two indexes. Index title to specify a section title and description. Index priority this is the partition priority, which determines their order.

Section Settings

Further, let's tweak some settings. We propose parameters for our section and consider 3 the option.

We declare the settings like this:

No. 3 setting option. On saving, an array will be created with the name, including the theme name and index, which will be the name of our option.

No. 2 setting option. When saving, a variable will be created. Variable name will include theme name and option name, which we will keep.

#1 setting option. When saving, a variable will be created, which in its name will store only the name of the option.

We will use option #3.

In addition to the fact that we have set the settings for the theme WordPress and with help add_setting put them in the database, still need to set controls. Let's try to add one of them, after adding, a section with controls such as radio buttons will appear in your Theme Customizer.

Consider displaying a checkbox control in Theme Customizer

Consider an example of displaying a select control in Theme Customizer

Consider an example of displaying a text input control in Theme Customizer

Consider an example of displaying an image upload control in Theme Customizer

Consider an example of displaying a control of the File Upload type in Theme Customizer

Consider an example of displaying a control of the Color Picker type in Theme Customizer

Consider an example of displaying a control of the Page Dropdown type in Theme Customizer

We are done with types., in general, as it turned out, not everything is so complicated and even object-oriented, which, to be honest, made me very happy WordPress, are gradually being corrected ). Think, to start developing your own themes with this tool will be enough.

Viewing and Displaying Options, which were saved in Theme Customizer

Live Preview

Oh yeah I almost forgot, options we have created, but we still need to display them in the template and allow the user to view our options before saving them, and only that person will be able to see the changes., who makes them in real time and a regular user will not be able to until the save button is pressed.

Most likely, many of the smart programmers who sit on my site have already guessed that to display the preview of the options, we will use the tricky JQUERY script.
At the very end of our function themename_customize_register, before closing, paste code:

This action will display the function in the footer of the site after executing the code, which will show the changes with JQUERY, changing the content of the element $( ‘#logo a’ ).html( newval ). Attention, my logo is marked ‘#logo a’ you may be different.

Code $wp_customize->get_setting( ‘…’)->transport = ‘postMessage’ as far as I understand, it is needed to pass the name of the option so that it can be further used to display a preview, I tried without this code but it didn't work. ), I didn't find the answer in English either.. If anyone has an answer, write in the comments. Even though the code works.

Here is the jquery plugin code:

Replace the code with this:

More about get_option read the article “Consider the mechanisms of working with options“.

Yes, thought the article would be shorter, but almost a complete guide to working with Theme Customizer. Looks like I got carried away with the topic. ).

Since the topic is not simple, I decided to lay out some sources for you.
zip wordpress настройка Theme Customizer


Buy WordPress hosting
WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

When copying a lesson, please leave a link back to the author's article, that is me!


Theme Customizer – settings in WordPress

First, to understand what Theme Customizer is, watch a video from YouTube, think, many things will become clear. After watching the video, you probably won't need a plugin OptionTree, which I talked about earlier.

Think, after watching, you will have many ideas how you can use this useful panel, and here's how it works, consider a little below.

Existing settings

The first thing I think is important to consider is the existing methods of setting up the theme, as many of their authors write 3 options, let's believe them and try to learn to use these methods.

  • There are fashions, which are supported to work with the theme, these mods can be controlled using functions set_theme_mod and get_theme_mod. These features are supported Settings API WordPress and work well.
  • Individual options for each topic. Not the best way to create themes, but also has a place to be and is supported in Theme Customizer.
  • Serialized Settings – Essentially, you store the settings in an array, and then save that array with set_option or get_option, as one row in the database. This is the most optimal method and we will consider it in this article.

We briefly mentioned the other two methods if we need them, we will consider them.
First of all, in The left part of the Theme Customizer is divided into sections and this is worth paying attention to! Everything works through special act (a pig) customize_register.

The function receives as a parameter $wp_customize it is the main object. We will work with it when adding sections, controls etc.

Sections in the Theme Customizer

so, the first, what to do, is to add a section. Here's one way to do it:

There are no controls in the section yet and they are not configured, the new section will not be displayed, we'll talk about the controls a little later, and now let's consider the code we described above.

themename_color_scheme – the first parameter is the unique identifier to display, we will need this ID later, when we will place the controls in the section.

The second parameter is an array. There are two indexes in this array. Index title to specify the section title and its description. Index priority is the priority of sections, which determines their order.

Section settings

Further, let's configure some parameters. Let's propose parameters for our section and consider 3 options.

We declare the settings like this:

No. 3 option of settings. When saved, an array with the name will be created, which includes the topic name and index, which will be the name of our option.

No. 2 option of settings. When saved, a variable will be created. The variable name will include the topic name and the option name, which we will keep.

No. 1 option of settings. When saved, a variable will be created, which will store only the option name in its name.

We will use option #3

in addition, that we have set the settings for the theme WordPress and with the help of add_setting recorded them in the database, you still need to set the controls. Let's try to add one of them, after adding in the Theme Customizer, a section with controls of the type of radio buttons will appear.

Consider the output of a checkbox control in the Theme Customizer

Let's consider an example of displaying a select control in the Theme Customizer

Let's consider an example of displaying a text input type control in the Theme Customizer

Consider an example of displaying an image upload control element in the Theme Customizer

Consider an example of displaying a File Upload type control in the Theme Customizer

Consider an example of displaying a Color Picker type control in the Theme Customizer

Consider an example of displaying a Page Dropdown control in the Theme Customizer

We are done with types, in general, not everything is so complicated and even object-oriented, which honestly made me very happy in WordPress, are being corrected little by little). Think, to start developing your own themes with this tool will be quite enough.

Viewing and displaying options, which were stored in the Theme Customizer

Live preview

Oh, I almost forgot, options we have created, but we still need to display them in the template and give the user the opportunity to view our options before saving them, at the same time, only he will be able to see the changes, who enters them in real time and cannot be saved by an ordinary user before pressing the button.

Most likely, there are many interpretive programmers, which are sitting on my website, already guessed, that we will use a clever JQUERY script to display a preview of the options.

At the end of our function themename_customize_register, before its closing, paste the code:

This action will display the function in the footer of the site after the code is executed, which will show the changes with JQUERY, changing the content of the element $(‘#logo a’).html(newval). Attention, the logo is marked '#logo a’ it may be different for you.

Code $wp_customize->get_setting( ‘…’)->transport = ‘postMessage’ as far as I understand is needed to pass the name of the option in this way, so that it can then be used to display the preview, tried without this code but nothing worked), I didn't particularly find an answer in English either. If anyone has an answer, write in the comments. Although the code still works.

Here is the JQUERY code of the plugin:

Replace the code with the following:

More about get_option read more “Let's consider the mechanisms of working with options“.

So, I thought the article would be shorter, but it turned out to be an almost complete guide to work with Theme Customizer. Apparently, fascinated by the topic ).

Since the topic is not simple, I decided to post the assembled code.
zip wordpress настройка Theme Customizer


Buy WordPress hosting
WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

When copying a lesson please leave a backlink to the author's article, i.e. me!

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal