Add Theme Support | Digging in WordPress Function

Add theme support <?php add_theme_support( $feature );  ?> function is very powerful function used by almost all themes and plugins to register support of a certain theme feature. You can add it in your theme’s function.php file or through plugin if attached to a hook. If attached to a hook, it must be after_setup_theme hook as The init hook may be too late for some features.

add theme support

With Add theme support function you can add the following features:

Note: custom-background and custom-header features will be available from WordPress version 3.4 onwards.

Post Thumbnails

This feature was added to add_theme_support() function in Version 2.9. This features enables Post Thumbnail (also known as Featured Images). This feature must be called before the init hook is fired. That means it needs to be placed directly into functions.php or within a function attached to the ‘after_setup_theme’ hook.

<?php add_theme_support( 'post-thumbnails' ); ?>

Note: You can optionally pass a second argument with an array of the Post Types for which you want to enable this feature. Check the example below:

add_theme_support( 'post-thumbnails' );
// Posts only
add_theme_support( 'post-thumbnails', array( 'post' ) );
// Pages only
add_theme_support( 'post-thumbnails', array( 'page' ) );
// Posts and Pages
add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );
// Posts and custom post type Portfolio
add_theme_support( 'post-thumbnails', array( 'post', 'portfolio' ) );

To display thumbnails in themes index.php or single.php or custom templates, use:

<?php the_post_thumbnail(); ?>

To check if there is a post thumbnail assigned to the post before displaying it, use:

<?php if ( has_post_thumbnail() ) {
	the_post_thumbnail();
} ?>

Feed Links

This feature was added to add_theme_support() function in Version 3.0 which replaced the depreciated automatic_feed_links() function. This feature enables post and comment RSS feed links to head.

<?php add_theme_support( 'automatic-feed-links' ); ?>

Post Formats

This feature was added to add_theme_support() function in Version 3.1. This feature enables Post Formats support for a Theme. You need to be careful while using Child Themes as add_theme_support( ‘post-formats’ ) will override the formats as defined by the parent theme.

To enable the specific formats use:

<?php add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote', 'image' ) ); ?>

List of Supported Formats

  • aside – Typically styled without a title. Similar to a Facebook note update.
  • gallery – A gallery of images. Post will likely contain a gallery shortcode and will have image attachments.
  • link – A link to another site. Themes may wish to use the first <a href=””> tag in the post content as the external link for that post. An alternative approach could be if the post consists only of a URL, then that will be the URL and the title (post_title) will be the name attached to the anchor for it.
  • image – A single image. The first <img /> tag in the post could be considered the image. Alternatively, if the post consists only of a URL, that will be the image URL and the title of the post (post_title) will be the title attribute for the image.
  • quote – A quotation. Probably will contain a blockquote holding the quote content. Alternatively, the quote may be just the content, with the source/author being the title.
  • status – A short status update, similar to a Twitter status update.
  • video – A single video. The first <video /> tag or object/embed in the post content could be considered the video. Alternatively, if the post consists only of a URL, that will be the video URL. May also contain the video as an attachment to the post, if video support is enabled on the blog (like via a plugin).
  • audio – An audio file. Could be used for Podcasting.
  • chat – A chat transcript, like so:

Custom Background

This feature will be added to add_theme_support() function in Version 3.4 which replaced the add_custom_background() function. For a time being you can use the add_custom_background() function but be careful and update it after the release of WordPress Version 3.4 as it will be depreciated then.

<?php add_theme_support( 'custom-background' ); ?>

This feature add_theme_support( ‘custom-background’, $args ) accepts an arguments array. Customize the arguments as per your needs and add it in your function.php:

<?php add_theme_support( 'custom-header', array(
	'default-color'          => '',
	'default-image'          => '',
	'wp-head-callback'       => '_custom_background_cb',
	'admin-head-callback'    => '',
	'admin-preview-callback' => ''
); ?>

Custom Header

This feature will be added to add_theme_support() function in Version 3.4 which replaced the use of add_custom_image_header() function. For a time being you can use the add_custom_image_header() function but be careful and update it after the release of WordPress Version 3.4 as it will be depreciated then.

<?php add_theme_support( 'custom-header' ); ?>

This feature add_theme_support( ‘custom-header’, $args ) accepts an arguments array. Customize the arguments as per your needs and add it in your function.php:

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

Example

Let’s see the real example of add theme support and how it have been implement in WordPress best and default “Twenty Eleven” theme:

<?php
add_action( 'after_setup_theme', 'twentyeleven_setup' );

if ( ! function_exists( 'twentyeleven_setup' ) ):

function twentyeleven_setup() {

	// This theme uses Featured Images (also known as post thumbnails) for per-post/per-page Custom Header images
	add_theme_support( 'post-thumbnails' );

	// Add default posts and comments RSS feed links to <head>.
	add_theme_support( 'automatic-feed-links' );

	// Add support for a variety of post formats
	add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote', 'image' ) );

	// Add support for custom backgrounds
	add_custom_background();

	// Add a way for the custom header to be styled in the admin panel that controls
	// custom headers. See twentyeleven_admin_header_style(), below.
	add_custom_image_header( 'twentyeleven_header_style', 'twentyeleven_admin_header_style', 'twentyeleven_admin_header_image' );
}
?>

If you have any concerns or queries, please write comments below.

1 thought on “Add Theme Support | Digging in WordPress Function

Leave a Reply

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