Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

_s: Update Custom Header Code. fixes #1212

* Use WordPress 3.4's registration method providing backward compatibility for previous versions.
* Implement shiv for get_custom_header(). props @obenland
* Allow all custom header args to be filtered via child theme/plugin.
* Update example code in DocBlock to use get_custom_header().



git-svn-id: https://wpcom-themes.svn.automattic.com/_s/@9484 d957f892-c61d-0410-b221-f235e6eecf30
  • Loading branch information...
commit 966679f25bdca86165c8b64f0364a461fc83052f 1 parent 2cd6317
Michael Fields authored April 23, 2012 ianstewart committed April 24, 2012

Showing 1 changed file with 64 additions and 16 deletions. Show diff stats Hide diff stats

  1. 80  inc/custom-header.php
80  inc/custom-header.php
@@ -8,7 +8,7 @@
8 8
 	<?php $header_image = get_header_image();
9 9
 	if ( ! empty( $header_image ) ) { ?>
10 10
 		<a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home">
11  
-			<img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="" />
  11
+			<img src="<?php header_image(); ?>" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" />
12 12
 		</a>
13 13
 	<?php } // if ( ! empty( $header_image ) ) ?>
14 14
 
@@ -17,30 +17,78 @@
17 17
  * @since _s 1.0
18 18
  */
19 19
 
  20
+/**
  21
+ * Setup the WordPress core custom header feature.
  22
+ *
  23
+ * Use add_theme_support to register support for WordPress 3.4+
  24
+ * as well as provide backward compatibility for previous versions.
  25
+ * Use feature detection of wp_get_theme() which was introduced
  26
+ * in WordPress 3.4.
  27
+ *
  28
+ * @uses _s_header_style()
  29
+ * @uses _s_admin_header_style()
  30
+ * @uses _s_admin_header_image()
  31
+ *
  32
+ * @package _s
  33
+ */
20 34
 function _s_custom_header_setup() {
21  
-	// The default header text color
22  
-	define( 'HEADER_TEXTCOLOR', '000' );
  35
+	$args = array(
  36
+		'default-image'          => '',
  37
+		'default-text-color'     => '000',
  38
+		'width'                  => 1000,
  39
+		'height'                 => 250,
  40
+		'flex-height'            => true,
  41
+		'wp-head-callback'       => '_s_header_style',
  42
+		'admin-head-callback'    => '_s_admin_header_style',
  43
+		'admin-preview-callback' => '_s_admin_header_image',
  44
+	);
23 45
 
24  
-	// By leaving empty, we allow for random image rotation.
25  
-	define( 'HEADER_IMAGE', '' );
  46
+	$args = apply_filters( '_s_custom_header_args', $args );
26 47
 
27  
-	// The height and width of your custom header.
28  
-	// Add a filter to _s_header_image_width and _s_header_image_height to change these values.
29  
-	define( 'HEADER_IMAGE_WIDTH', apply_filters( '_s_header_image_width', 1000 ) );
30  
-	define( 'HEADER_IMAGE_HEIGHT', apply_filters( '_s_header_image_height', 250 ) );
  48
+	if ( function_exists( 'wp_get_theme' ) ) {
  49
+		add_theme_support( 'custom-header', $args );
  50
+	} else {
  51
+		// Compat: Versions of WordPress prior to 3.4.
  52
+		define( 'HEADER_TEXTCOLOR',    $args['default-text-color'] );
  53
+		define( 'HEADER_IMAGE',        $args['default-image'] );
  54
+		define( 'HEADER_IMAGE_WIDTH',  $args['width'] );
  55
+		define( 'HEADER_IMAGE_HEIGHT', $args['height'] );
  56
+		add_custom_image_header( $args['wp-head-callback'], $args['admin-head-callback'], $args['admin-preview-callback'] );
  57
+	}
  58
+}
  59
+add_action( 'after_setup_theme', '_s_custom_header_setup' );
31 60
 
32  
-	// Turn on random header image rotation by default.
33  
-	add_theme_support( 'custom-header', array( 'random-default' => true ) );
  61
+/**
  62
+ * Shiv for get_custom_header().
  63
+ *
  64
+ * get_custom_header() was introduced to WordPress
  65
+ * in version 3.4. To provide backward compatibility
  66
+ * with previous versions, we will define our own version
  67
+ * of this function.
  68
+ *
  69
+ * @return stdClass All properties represent attributes of the curent header image.
  70
+ *
  71
+ * @package _s
  72
+ * @since _s 1.1
  73
+ */
34 74
 
35  
-	// Add a way for the custom header to be styled in the admin panel that controls custom headers
36  
-	add_custom_image_header( '_s_header_style', '_s_admin_header_style', '_s_admin_header_image' );
  75
+if ( ! function_exists( 'get_custom_header' ) ) {
  76
+	function get_custom_header() {
  77
+		return (object) array(
  78
+			'url'           => get_header_image(),
  79
+			'thumbnail_url' => get_header_image(),
  80
+			'width'         => HEADER_IMAGE_WIDTH,
  81
+			'height'        => HEADER_IMAGE_HEIGHT,
  82
+		);
  83
+	}
37 84
 }
38  
-add_action( 'after_setup_theme', '_s_custom_header_setup' );
39 85
 
40 86
 if ( ! function_exists( '_s_header_style' ) ) :
41 87
 /**
42 88
  * Styles the header image and text displayed on the blog
43 89
  *
  90
+ * @see _s_custom_header_setup().
  91
+ *
44 92
  * @since _s 1.0
45 93
  */
46 94
 function _s_header_style() {
@@ -80,7 +128,7 @@ function _s_header_style() {
80 128
 /**
81 129
  * Styles the header image displayed on the Appearance > Header admin panel.
82 130
  *
83  
- * Referenced via add_custom_image_header() in _s_setup().
  131
+ * @see _s_custom_header_setup().
84 132
  *
85 133
  * @since _s 1.0
86 134
  */
@@ -110,7 +158,7 @@ function _s_admin_header_style() {
110 158
 /**
111 159
  * Custom header image markup displayed on the Appearance > Header admin panel.
112 160
  *
113  
- * Referenced via add_custom_image_header() in _s_setup().
  161
+ * @see _s_custom_header_setup().
114 162
  *
115 163
  * @since _s 1.0
116 164
  */

2 notes on commit 966679f

Konstantin Obenland
Collaborator

Wow, this is awesome! Thx!

Michael Fields

@obenland Thanks for the example code! I was thinking in a totally different direction until I saw your post on the WPTRT mailing list. Good stuff :)

Please sign in to comment.
Something went wrong with that request. Please try again.