Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Difference between revisions of "Custom Headers"

m (link to Appearance Header Screen, Ja Codex)
(Undo revision 170977 by Vincentmurimi254 (talk))
 
(15 intermediate revisions by 12 users not shown)
Line 1: Line 1:
 
{{Languages|
 
{{Languages|
 
{{en|Custom Headers}}
 
{{en|Custom Headers}}
  +
{{fr|Custom Headers}}
  +
{{hr|Prilagodljiva Zaglavlja}}
  +
{{ru|Персонализация Шапки}}
 
{{ja|Custom Headers}}
 
{{ja|Custom Headers}}
  +
{{pt-br|Cabeçalhos_Personalizados}}
  +
{{zh-cn|自定义顶部}}
 
}}
 
}}
   
Line 10: Line 15:
 
== Adding Theme Support ==
 
== Adding Theme Support ==
   
Since [[Version 3.4]], themes need to use [[Function_Reference/add_theme_support|add_theme_support()]] in the [[Theme_Development#Functions_File|functions.php]] file to supports custom headers, like so:
+
Since [[Version 3.4]], themes need to use [[Function_Reference/add_theme_support|add_theme_support()]] in the [[Theme_Development#Functions_File|functions.php]] file to support custom headers, like so:
   
 
<pre>add_theme_support( 'custom-header' );</pre>
 
<pre>add_theme_support( 'custom-header' );</pre>
Line 19: Line 24:
 
$defaults = array(
 
$defaults = array(
 
'default-image' => '',
 
'default-image' => '',
'random-default' => false,
 
 
'width' => 0,
 
'width' => 0,
 
'height' => 0,
 
'height' => 0,
 
'flex-height' => false,
 
'flex-height' => false,
 
'flex-width' => false,
 
'flex-width' => false,
'default-text-color' => '',
 
'header-text' => true,
 
 
'uploads' => true,
 
'uploads' => true,
 
'random-default' => false,
 
'header-text' => true,
 
'default-text-color' => '',
 
'wp-head-callback' => '',
 
'wp-head-callback' => '',
 
'admin-head-callback' => '',
 
'admin-head-callback' => '',
Line 33: Line 38:
 
add_theme_support( 'custom-header', $defaults );
 
add_theme_support( 'custom-header', $defaults );
 
</pre>
 
</pre>
  +
  +
You must also [[Function_Reference/register_default_headers|register the header image]] before it can be used as a default in your theme.
   
 
== Example ==
 
== Example ==
Line 71: Line 78:
 
'flex-width' => true,
 
'flex-width' => true,
 
'width' => 980,
 
'width' => 980,
'flex-width' => true,
+
'flex-height' => true,
 
'height' => 200,
 
'height' => 200,
 
'default-image' => get_template_directory_uri() . '/images/header.jpg',
 
'default-image' => get_template_directory_uri() . '/images/header.jpg',
Line 83: Line 90:
 
<img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="" />
 
<img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="" />
 
</pre>
 
</pre>
  +
  +
== Backwards Compatibility ==
  +
  +
To add backwards compatibility for older versions, use the following code:
  +
  +
<pre>
  +
global $wp_version;
  +
if ( version_compare( $wp_version, '3.4', '>=' ) ) :
  +
add_theme_support( 'custom-header' );
  +
else :
  +
add_custom_image_header( $wp_head_callback, $admin_head_callback );
  +
endif;
  +
</pre>
  +
  +
== Resources ==
  +
  +
* [http://generatewp.com/theme-support/ WordPress Theme Support Generator]
   
 
== Related ==
 
== Related ==
 
{{Theme Support}}
 
{{Theme Support}}
  +
  +
[[Category:Functions]]
  +
*Function: [[header_image]]
  +
*Function: [[get_header_image]]
  +
*Function: [[get_custom_header]]
   
 
[[Category:Advanced Topics]]
 
[[Category:Advanced Topics]]

Latest revision as of 20:54, 11 September 2021

Custom Header is a theme feature introduced with Version 2.1. Custom header is an image that is chosen as the representative image in the theme top header section.

See also Appearance Header Screen.

Adding Theme Support

Since Version 3.4, themes need to use add_theme_support() in the functions.php file to support custom headers, like so:

add_theme_support( 'custom-header' );

Note that you can add default arguments using:

$defaults = 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', $defaults );

You must also register the header image before it can be used as a default in your theme.

Example

Set a custom header image

Set a default header image 980px width and 60px height:

$args = array(
	'width'         => 980,
	'height'        => 60,
	'default-image' => get_template_directory_uri() . '/images/header.jpg',
);
add_theme_support( 'custom-header', $args );

Upload other custom header images

Set a default header image and allow the site owner to upload other images:

$args = array(
	'width'         => 980,
	'height'        => 60,
	'default-image' => get_template_directory_uri() . '/images/header.jpg',
	'uploads'       => true,
);
add_theme_support( 'custom-header', $args );

Use flexible headers

Set flexible headers:

$args = array(
	'flex-width'    => true,
	'width'         => 980,
	'flex-height'    => true,
	'height'        => 200,
	'default-image' => get_template_directory_uri() . '/images/header.jpg',
);
add_theme_support( 'custom-header', $args );

update your header.php file to:

<img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="" />

Backwards Compatibility

To add backwards compatibility for older versions, use the following code:

global $wp_version;
if ( version_compare( $wp_version, '3.4', '>=' ) ) :
	add_theme_support( 'custom-header' );
else :
	add_custom_image_header( $wp_head_callback, $admin_head_callback );
endif;

Resources

Related

Theme Support: add_theme_support(), remove_theme_support(), current_theme_supports()
Theme Features: sidebar, menus, post-formats, title-tag, custom-background, custom-header, custom-logo, post-thumbnails, automatic-feed-links, html5, editor-style, content_width