Part 6: How to add Features in WordPress Using add_theme_support() ?

Wondering about this add_theme_support() function and how to use it to add_theme features. Nothing to worry, we are here to help you out to get sorted with the query. Read this article to understand how to add Theme features with add_theme_support. 

To register support for certain theme features in WordPress, the add_theme_function needs to be added. It seems to be a very powerful function, and almost all the theme developers use this function.

If you are customizing your theme or creating a theme from scratch, then it is important to know about this function and how to use it. But don’t take it hard. This function is very easy to use even for the beginners can understand.

Web Hosting

In WordPress by default, there is a list of theme support functionality. This function is mainly used to implement some special features inside the custom theme.

Adding theme features in WordPress using add_theme_support

In order to create a theme from scratch, customizing the theme, or if you are a beginner for developing a theme, then you must definitely know about this add_theme_support function. 

This add_theme_support() function is a pre-built hook in WordPress where you can easily activate theme support with the specified features. The general syntax of add_theme_support is

add_theme_support(string $feature);

The list of features you can include in the add_theme_support function. These features will work only inside the functions.php file. The add_theme_support function will work without any action.

Note: If you want to use this function outside the functions.php file for any plugins you have to specify a hook called ‘after_theme_setup’.

  • Post-Formats→ This feature lets you know what type of post to be incorporated into your themes such as a gallery, aside, chat, link, image, quotes, status, video, and audio.
  • Custom Background→ This features lets to incorporate custom background. You can include default arrays in the custom background such as color, image, size, callback, repeat, attachment, position, etc.
  • Custom Logo→ these features enable you to specify your custom logo. You can include default arrays in the custom logo such as height, width, header-text, etc.
  • Custom Header→ This feature allows you to include the custom header. You can include default arrays in the custom header such as width, height, image, upload, random-default, header-text, etc.
  • Post Thumbnails→ This feature is actually used to display the featured images for a post or a page. You will be able to display this feature through the entire website with a variety of manners. You can choose the dimensions you want the post thumbnails to display.
  • HTML5→ This feature is mainly used to enable the HTML5 for the comment form, gallery, search form, caption, and comment list.
  • Customize Selective Refresh Widgets→ By enabling this feature, you will be able to selectively refresh the widgets, instead of refreshing your entire page when you are working with the customizer.
  • Title Tag→ By enabling this feature, it lets your theme to control the title-tag. It will be added in the <head>.
  • Feed Links→ This feature is mainly used to activate the automatic feed links so that the feed links can be used for RSS feeds for any posts and comments in the <head>.

Specifying add_theme_support() for each feature

1.Post Formats

add_theme_support(‘post format’);

Adding parameters to post format with an array

add_theme_support( ‘post-formats’, array(
‘gallery’,
‘quote’,
‘video’,
‘aside’,
‘image’,
‘link’,
‘status’,
‘audio’,
‘chat’
) );

 

2. Custom Background

add_theme_support(‘custom-background’);

custom background

Specifying parameters for custom background

$def = array(
‘default-color’          => '',
‘default-image’          => '',
‘default-repeat’         => ‘repeat’,
‘default-position-x’     => ‘left’,
       ‘default-position-y’    => ’top’,
       ‘default-size’           => ‘auto’,
‘default-attachment’     => ‘scroll’,
‘wp-head-callback’       => ‘_custom_background_cb’,
‘admin-head-callback’    => '',
‘admin-preview-callback’ => ''
);
add_theme_support( ‘custom-background’, $def );

 

3.Custom Logo

add_theme_support(‘custom-logo’);

 

Specifying parameters

add_theme_support( ‘custom-logo’, array(
‘height’      => 100,
‘width’       => 400,
‘flex-height’ => true,
‘flex-width’  => true,
‘header-text’ => array( ‘site-title’, ‘site-description’ ),
) );

 

4.Custom Header

add_theme_support(custom-header’);

Custom Header

Specifying parameters

$def = array(
‘default-image’          => '',
‘width’                  => 0,
‘height’                 => 0,
‘flex-height’            => false,
‘flex-width’             => false,
‘uploads’                => true,
‘random-default’         => false,
‘header-text’            => true,
‘default-text-color’     => '',
‘wp-head-callback’       => '',
‘admin-head-callback’    => '',
‘admin-preview-callback’ => '',
);
add_theme_support( ‘custom-header’, $def );

 

Adding parameters to post format in WordPress

5.Post Thumbnails

add_theme_support( ‘post-thumbnails’ );

 

Passing arguments

add_theme_support( ‘post-thumbnails’, array( ‘post’ ) ); // posts only
add_theme_support( ‘post-thumbnails’, array( ‘page’ ) ); // pages only

add_theme_support( ‘post-thumbnails’, array( ‘your-post-type-name’ ) ); // custom post types

 

6.HTML5

add_theme_support( ‘html5’, array( ‘comment-list’, ‘comment-form’, ‘search-form, ‘gallery’, ‘caption’ ) );

 

7.Customize Selective Refresh Widgets

add_theme_support( ‘customize-selective-refresh-widgets’ );

 

8.Title Tag

add_theme_support( ‘title-tag’ );

 

9.Feed Links

add_theme_support( ‘automatic-feed-links’ );

A below example shows adding post thumbnails, HTML5, and post links. Place the following code in the functions.php file.

Custom Header

function wpblogx_theme_feature() {

 /* post formats */
 add_theme_support( ‘post-formats’, array( ‘aside’, ‘quote ) );
 
 /* post thumbnails */
 add_theme_support( 'post-thumbnails', array( ‘post’, ‘page’ ) );

 /* HTML5 */
 add_theme_support( ‘html5’ );

 /* automatic feed links */
 add_theme_support( ‘automatic-feed-links’ );

}
add_action( ‘after_setup_theme’, ‘wpblogx_theme_feature’ );

You can add these features together as well to represent your theme in a good manner. Hope you got an idea about how to add these features with add_theme_support function.

You may also like this article

If you have any queries or suggestions, please feel free to comment to us. You can subscribe to us on Facebook and Twitter.

Editorial Staff

Prabhu Ganesan has provided his Web development and SEO services independently. He is Co-founder of Webicle Media and WPBlogX. He is very much involved in WordPress as it becomes a second nature for him. Find him on twitter @gprabhucbe

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.