add_theme_support

Registers theme support for a given feature.

Description

(void|bool) add_theme_support( (string) $feature ); 

Must be called in the theme's functions.php file to work. If attached to a hook, it must be after_setup_theme. The init hook may be too late for some features.

Returns (void|bool)

False on failure, void otherwise.

Parameters (1)

0. $feature (string)
The feature being added.

Usage

  1. if ( !function_exists( 'add_theme_support' ) ) { 
  2. require_once ABSPATH . WPINC . '/theme.php'; 
  3.  
  4. // The feature being added. 
  5. $feature = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = add_theme_support($feature); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/theme.php  
  1. function add_theme_support( $feature ) { 
  2. global $_wp_theme_features; 
  3.  
  4. if ( func_num_args() == 1 ) 
  5. $args = true; 
  6. else 
  7. $args = array_slice( func_get_args(), 1 ); 
  8.  
  9. switch ( $feature ) { 
  10. case 'post-formats' : 
  11. if ( is_array( $args[0] ) ) { 
  12. $post_formats = get_post_format_slugs(); 
  13. unset( $post_formats['standard'] ); 
  14.  
  15. $args[0] = array_intersect( $args[0], array_keys( $post_formats ) ); 
  16. break; 
  17.  
  18. case 'html5' : 
  19. // You can't just pass 'html5', you need to pass an array of types. 
  20. if ( empty( $args[0] ) ) { 
  21. // Build an array of types for back-compat. 
  22. $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) ); 
  23. } elseif ( ! is_array( $args[0] ) ) { 
  24. _doing_it_wrong( "add_theme_support( 'html5' )", __( 'You need to pass an array of types.' ), '3.6.1' ); 
  25. return false; 
  26.  
  27. // Calling 'html5' again merges, rather than overwrites. 
  28. if ( isset( $_wp_theme_features['html5'] ) ) 
  29. $args[0] = array_merge( $_wp_theme_features['html5'][0], $args[0] ); 
  30. break; 
  31.  
  32. case 'custom-logo': 
  33. if ( ! is_array( $args ) ) { 
  34. $args = array( 0 => array() ); 
  35. $defaults = array( 
  36. 'width' => null,  
  37. 'height' => null,  
  38. 'flex-width' => false,  
  39. 'flex-height' => false,  
  40. 'header-text' => '',  
  41. ); 
  42. $args[0] = wp_parse_args( array_intersect_key( $args[0], $defaults ), $defaults ); 
  43.  
  44. // Allow full flexibility if no size is specified. 
  45. if ( is_null( $args[0]['width'] ) && is_null( $args[0]['height'] ) ) { 
  46. $args[0]['flex-width'] = true; 
  47. $args[0]['flex-height'] = true; 
  48. break; 
  49.  
  50. case 'custom-header-uploads' : 
  51. return add_theme_support( 'custom-header', array( 'uploads' => true ) ); 
  52.  
  53. case 'custom-header' : 
  54. if ( ! is_array( $args ) ) 
  55. $args = array( 0 => array() ); 
  56.  
  57. $defaults = array( 
  58. 'default-image' => '',  
  59. 'random-default' => false,  
  60. 'width' => 0,  
  61. 'height' => 0,  
  62. 'flex-height' => false,  
  63. 'flex-width' => false,  
  64. 'default-text-color' => '',  
  65. 'header-text' => true,  
  66. 'uploads' => true,  
  67. 'wp-head-callback' => '',  
  68. 'admin-head-callback' => '',  
  69. 'admin-preview-callback' => '',  
  70. ); 
  71.  
  72. $jit = isset( $args[0]['__jit'] ); 
  73. unset( $args[0]['__jit'] ); 
  74.  
  75. // Merge in data from previous add_theme_support() calls. 
  76. // The first value registered wins. (A child theme is set up first.) 
  77. if ( isset( $_wp_theme_features['custom-header'] ) ) 
  78. $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] ); 
  79.  
  80. // Load in the defaults at the end, as we need to insure first one wins. 
  81. // This will cause all constants to be defined, as each arg will then be set to the default. 
  82. if ( $jit ) 
  83. $args[0] = wp_parse_args( $args[0], $defaults ); 
  84.  
  85. // If a constant was defined, use that value. Otherwise, define the constant to ensure 
  86. // the constant is always accurate (and is not defined later, overriding our value). 
  87. // As stated above, the first value wins. 
  88. // Once we get to wp_loaded (just-in-time), define any constants we haven't already. 
  89. // Constants are lame. Don't reference them. This is just for backwards compatibility. 
  90.  
  91. if ( defined( 'NO_HEADER_TEXT' ) ) 
  92. $args[0]['header-text'] = ! NO_HEADER_TEXT
  93. elseif ( isset( $args[0]['header-text'] ) ) 
  94. define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) ); 
  95.  
  96. if ( defined( 'HEADER_IMAGE_WIDTH' ) ) 
  97. $args[0]['width'] = (int) HEADER_IMAGE_WIDTH
  98. elseif ( isset( $args[0]['width'] ) ) 
  99. define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] ); 
  100.  
  101. if ( defined( 'HEADER_IMAGE_HEIGHT' ) ) 
  102. $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT
  103. elseif ( isset( $args[0]['height'] ) ) 
  104. define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] ); 
  105.  
  106. if ( defined( 'HEADER_TEXTCOLOR' ) ) 
  107. $args[0]['default-text-color'] = HEADER_TEXTCOLOR
  108. elseif ( isset( $args[0]['default-text-color'] ) ) 
  109. define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] ); 
  110.  
  111. if ( defined( 'HEADER_IMAGE' ) ) 
  112. $args[0]['default-image'] = HEADER_IMAGE
  113. elseif ( isset( $args[0]['default-image'] ) ) 
  114. define( 'HEADER_IMAGE', $args[0]['default-image'] ); 
  115.  
  116. if ( $jit && ! empty( $args[0]['default-image'] ) ) 
  117. $args[0]['random-default'] = false; 
  118.  
  119. // If headers are supported, and we still don't have a defined width or height,  
  120. // we have implicit flex sizes. 
  121. if ( $jit ) { 
  122. if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) ) 
  123. $args[0]['flex-width'] = true; 
  124. if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) ) 
  125. $args[0]['flex-height'] = true; 
  126.  
  127. break; 
  128.  
  129. case 'custom-background' : 
  130. if ( ! is_array( $args ) ) 
  131. $args = array( 0 => array() ); 
  132.  
  133. $defaults = array( 
  134. 'default-image' => '',  
  135. 'default-repeat' => 'repeat',  
  136. 'default-position-x' => 'left',  
  137. 'default-attachment' => 'scroll',  
  138. 'default-color' => '',  
  139. 'wp-head-callback' => '_custom_background_cb',  
  140. 'admin-head-callback' => '',  
  141. 'admin-preview-callback' => '',  
  142. ); 
  143.  
  144. $jit = isset( $args[0]['__jit'] ); 
  145. unset( $args[0]['__jit'] ); 
  146.  
  147. // Merge in data from previous add_theme_support() calls. The first value registered wins. 
  148. if ( isset( $_wp_theme_features['custom-background'] ) ) 
  149. $args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] ); 
  150.  
  151. if ( $jit ) 
  152. $args[0] = wp_parse_args( $args[0], $defaults ); 
  153.  
  154. if ( defined( 'BACKGROUND_COLOR' ) ) 
  155. $args[0]['default-color'] = BACKGROUND_COLOR
  156. elseif ( isset( $args[0]['default-color'] ) || $jit ) 
  157. define( 'BACKGROUND_COLOR', $args[0]['default-color'] ); 
  158.  
  159. if ( defined( 'BACKGROUND_IMAGE' ) ) 
  160. $args[0]['default-image'] = BACKGROUND_IMAGE
  161. elseif ( isset( $args[0]['default-image'] ) || $jit ) 
  162. define( 'BACKGROUND_IMAGE', $args[0]['default-image'] ); 
  163.  
  164. break; 
  165.  
  166. // Ensure that 'title-tag' is accessible in the admin. 
  167. case 'title-tag' : 
  168. // Can be called in functions.php but must happen before wp_loaded, i.e. not in header.php. 
  169. if ( did_action( 'wp_loaded' ) ) { 
  170. /** translators: 1: Theme support 2: hook name */ 
  171. _doing_it_wrong( "add_theme_support( 'title-tag' )", sprintf( __( 'Theme support for %1$s should be registered before the %2$s hook.' ),  
  172. '<code>title-tag</code>', '<code>wp_loaded</code>' ), '4.1' ); 
  173.  
  174. return false; 
  175.  
  176. $_wp_theme_features[ $feature ] = $args;