Add WordPress customizer fields using Customizer API

We are getting a lot of requests for a tutorial on how to Add WordPress customizer fields using Customizer API. This will be a handy tutorial for the WordPress developer who loves adding WordPress customizer fields using Customizer API.

For those who are new to WordPress and don’t know what is WordPress customizer fields and what is Customizer API, we will explain this a bit. If you already know this then you can skip the following paragraph and directly jump to the coding part.

What are WordPress customizer fields?

If you ever opened the customize option under appearance then you might notice some fields (input, textarea, image upload, etc) under different sections like header settings. These settings are generally made by the theme. See the screenshot for your reference

WordPress customizer fields
WordPress customizer fields

For information on WordPress Customizer API, you can visit this link

They have nice documentation but for practically adding the code, you can ready throughout this tutorial. By the end of this tutorial, you will be able to add any customized field using WordPress Customize API.

In this tutorial, we will use a real-life example which is a very common requirement for almost all projects.

We will create a social link section that you can use anywhere on the website. In general, this is used in the header or footer of the website but it’s up to your requirement to use this anywhere you want.

Lets Start

Step 1 – Add customize_register action hook

First of all, we have to add a customize_register action hook which allows us to add the options for WordPress customize API. Add the following code to your active theme’s functions.php file

Line number 1 is the action hook name “customize_register” and the second parameter is the name of the callback function. We have defined a blank function for a callback at line number 6 which we will fill section by section.

Step 2 – add a panel and section

Panels and sections are the wrappers for WordPress customizer fields. In the screenshot above, “Site Identity” is the section. To add this, copy and paste the following code in the above empty callback function.

Step 3 – Add settings and controls

Now we have a section ready with the name “Header section”. It’s time to add the fields inside this section. We will add three fields for

  1. Facebook url
  2. Twitter url
  3. LinkedIn url

Add the following code to the same callback function under the above code.

Up to this, your “Header section” is ready with all the three fields to be saved in the database.

Still, confused with adding code? Let me make it a bit more easy for you. Just add the following code to your active theme’s functions.php file

Now we are ready with data stored for Facebook, Twitter, and LinkedIn URLs. It’s time to show the links as per this data. For that, we will use a shortcode that you can place anywhere you like.

Step 4 – Add shortcode

Simply add the following code to the active theme’s functions.php  to add WordPress customizer fields data in the social URL links

The above code will add a shortcode with the name “dcs_social_links”.

Step 5 – Place shortcode – dcs_social_links

Place the shortcode dcs_social_links where you want to show the social links.