rarely find a WordPress template that does not use the add_theme_support function, which is considered one of the effective and easy-to-use functions included in the WordPress system.
This function basically adds or registers certain pre-defined features within a WordPress theme (Theme Functionality) and allows you to customize them to your liking and manage them through the theme customization page.
If you are a WordPress template developer and want to create a template from scratch, or want to modify a specific template and customize it according to your requirements, it is necessary for you to know the function of this function, know how to use it, and the features it provides so that you can support your WordPress template with these features and customize it according to your requirements. .
What is the add_theme_support() function
The add_theme_support() function is a built-in WordPress function used to support WordPress themes with specific features.
As you can see from the code above, the function has two parameters or arguments.
The first parameter $feature is mandatory and is a string representing the name of the feature to be supported in the template.
The second parameter, $args, is optional. It represents an array of parameters whose element values vary according to the feature specified in the first parameter. Through this array, we can customize the features and make them more specific.
For example, the following code is used to support the two title tag features to provide a title for search engines that is different from the article title, and a custom logo feature for the template and specifying its dimensions to be 100 x 100 pixels.
Features that can be provided through the add_theme_support function
Now you might be wondering what built-in features I can add to my theme through add_theme_support function?
In fact, there are about thirty implicit features that a WordPress template can support through this function, and features are added with different versions of WordPress, and the most important of these features are:
- custom-logo Enable the website logo customization feature on the template customization page. This feature was first supported in WordPress version 4.5.
- custom-header Enables the header customization feature, which enables you, for example, to upload a specific image and display it in the header of your template, or specify the height and width of the header.
- custom-background : The feature of customizing the background image. Supporting this feature will add two sections to the template customization page: colors and background image. Thus, you can set the background color or background image for your template.
- post-formats : Provides the feature of formatting articles according to a specific form, content, and style within the template according to the type of content that this article displays.
This feature supports customizing 9 content types that can be passed through the feature matrix as follows:
- post-thumbnails : Providing the feature of customizing a distinctive image for all types of posts
You can pass an optional parameter that represents an array specifying the types of posts in which you want to enable this feature (posts, pages, custom post type ) as follows
- HTML5 feature supports the use of HTML5 codes in some parts of the template, such as the search form, comments form, and gallery..
- starter-content: Providing a starter content feature for WordPress from the beginning.
- automatic-feed-links The feature of activating automatic RSS feed links so that RSS feed links can be used for any posts and comments in the <head> tag.
- title-tag Supports the title tag, which is usually placed in the <head> tag of the page, and which is used by the browser to dynamically determine the title of the web page.
- editor-font-sizes: This feature allows us to set values for font sizes when editing paragraph texts in the Gutenberg component editor.
- disable-custom-font-sizes : This feature prevents us from selecting or entering specific font size values in a section when editing paragraphs in the Gutenberg plug-in editor
- editor-color-palette : This feature allows us to adjust the colors that appear in the Color Settings section when editing paragraphs in the component editor.
- disable-custom-color-palette : This feature removes the custom color selection button that appears in the Color Settings section when editing paragraphs in the component editor.
- wp-block-styles : The component editor includes some default CSS values but these CSS values are not used in your theme, so you can add this feature to ensure that the CSS values added to customize components are used in your theme as well.
- dark-editor-style: This feature adjusts the interface of the component editor if you use a dark background style for your component editor.
- responsive-embeds : This feature makes your theme display files embedded in content (such as YouTube videos) added using the component editor responsive to mobile screens by adding a row named wp-embed-response.
- Customize Selective Refresh Widgets: This feature enables you to update interface elements, or what are known as widgets, only through the template customization page instead of updating your entire page.
Where do we write the code to call the add_theme_support function?
You can write the code to call this function directly into the template’s functions.php file, and then it will be called automatically when the template is executed without the need to link it to any hook in order to execute it.
You can also link its call to action hooks to be executed when this hook occurs. The appropriate hook in this case is the after_setup_theme hook , which occurs after the template is loaded so that the function works correctly.
This hook allows adding theme support for any feature. It works as soon as the theme is set up. WordPress does not allow us to support theme features until WordPress has finished setting up the theme internally. So to support a feature in your theme all you have to do is link this action hook to the function supporting the feature you want.
For example, you can call the function code in order to enable the custom logo feature in a WordPress theme:
Read more: Explanation of WordPress Hooks and their importance for theme and plugin developers
Note 1:
If you want to call this function from a custom plugin and not within the theme’s functions file, then you must definitely hook it to the after_setup_theme action hook for it to execute correctly.
Note 2:
Calling this function within the template functions file will only enable it in the back-end of the site, but you must write code to display it and customize the way it appears in the front-end of the site. In the following paragraph, we will explain this in practical application to one of the features, which is the feature of providing a custom logo for a WordPress template, to better clarify the idea.
Note 3:
When one of the following parameters is used within the function (sidebars, menus, editor-style), the function is not called from the template’s functions file, but rather it is called only to verify that the template supports these features within the context of the current_theme_supports function , because these parameters are read-only.
A practical application for adding a custom logo feature to a WordPress template
Suppose you have a simple template or a template under development on your site, and this template does not support the feature of displaying a logo or logo customized for the site.
That is, when you go to the site admin control panel > and choose Appearance > Customize > Site Identity, you notice that there is no option to specify a custom logo for the site in your template, as shown in the following image:
To support this feature in the template, you must first add the following code to the template functions file functions.php:
If you want to further customize the logo, such as making it with specific dimensions, you can, as we explained before, pass values to the customization matrix as a second parameter to the add_theme_support function. For example, the desired width and height of the logo can be set as follows:
After adding this code to the functions.php file, and moving again to your site’s control panel > Appearance > Customization > Site Identity, you will notice that there is a new option called Logo in the Site Identity section, and below it is a button that enables you to specify and choose a custom logo for your site, as shown in the following image:
Upload the appropriate logo image and click the Publish button to save the changes. You will notice that the custom logo is not yet displayed on the front-end of the site!
You need to continue working and displaying the logo on the front end by calling the_custom_logo() function, but it is better to test the existence of this function before calling it to maintain compatibility with older versions of WordPress, as follows:
You can call this code anywhere within the template where you want to display the logo you uploaded, but a common place to call it is within the template’s header file, header.php.
We browse the current template files, find the header.php file and open it in any code editor. The header file for my current site template displays the menu in the site header (under the nav tag) as follows:
Therefore, I will add the logo display code within this nav tag and before the list display code, so that it appears in the position indicated by an arrow in the following image:
The complete code for the site header will be as follows:
Now, if you browse the site, you will find that the logo you downloaded appears in the appropriate place as follows
Include all template features in one function
When your template includes more than one feature, you can group these features together and add them within one function, and call this function through the after_setup_theme action hook. This will make your template code organized in a better way, as shown in the following code:
Conclusion
In today’s article, we learned how to add features to our WordPress template by using the ready-made function add_theme_support. We also attached a practical example of using this feature to customize the WordPress website logo through the template customization tool.
Finally, we explained how to call it within a WordPress template in the optimal way. If you have any questions about this function or one of the features it provides to a WordPress template, do not hesitate to ask your question in the comments below the article.
Leave a Reply