Updating Custom Backgrounds and Custom Headers for WordPress 3.4
When WordPress 3.4 is released, the old implementation methods for custom backgrounds and custom image headers via add_custom_background() and add_custom_image_header() will be deprecated, in favor of a much simpler – and much more powerful – implementation method, using add_theme_support(). Here’s how to update your existing Themes to use the new implementation.
Custom Backgrounds
Old method:
add_custom_background();
New method:
add_theme_support( 'custom-background' );
(Admit it: that was easy, wasn’t it?)
Now for the fun part: add_theme_support( 'custom-background', $args ). What’s that: $args, you say? Yes! This method accepts an arguments array. Here are the defaults:
$defaults = array( 'default-image' => '', 'default-color' => '', 'wp-head-callback' => '_custom_background_cb', 'admin-head-callback' => '', 'admin-preview-callback' => '' )
Defining a default background image or default background color just became dead-simple:
add_theme_support( 'custom-background', array( // Background color default 'default-color' => '000', // Background image default 'default-image' => get_template_directory_uri() . '/images/background.jpg' ) );
You may notice that the arguments array also includes the same callbacks that have always been available for custom image header implementation, so you also have more control over the front-end style definitions as well as the admin display.
Custom Headers
Old method:
// Define default header image constant define( 'HEADER_IMAGE', get_template_directory_uri() . '/images/headers/default.jpg' ); // Define header image width constant define( 'HEADER_IMAGE_WIDTH', 1000 ); // Define header image height constant define( 'HEADER_IMAGE_HEIGHT', 198 ); // Define header text constant define( 'NO_HEADER_TEXT', false ); // Define header text color constant define( 'HEADER_TEXTCOLOR', '000' ); // Turn on random header image rotation by default. // Requires HEADER_IMAGE to be null add_theme_support( 'custom-header', array( 'random-default' => true ) ); // Add Theme support add_custom_image_header( $wphead_cb, $adminhead_cb, $adminpreview_cb );
New method:
add_theme_support( 'custom-header', array( // Header image default 'default-image' => get_template_directory_uri() . '/images/headers/default.jpg', // Header text display default 'header-text' => false, // Header text color default 'default-text-color' => '000', // Header image width (in pixels) 'width' => 1000, // Header image height (in pixels) 'height' => 198, // Header image random rotation default 'random-default' => false, // Template header style callback 'wp-head-callback' => $wphead_cb, // Admin header style callback 'admin-head-callback' => $adminhead_cb, // Admin preview style callback 'admin-preview-callback' => $adminpreview_cb ) );
Again: that was easy, wasn’t it?
Just to clarify, here are the old-constant/new-array-key equivalents:
HEADER_IMAGE => 'default-image' HEADER_IMAGE_WIDTH => 'width' HEADER_IMAGE_HEIGHT => 'height' NO_HEADER_TEXT => 'header-text' HEADER_TEXTCOLOR => 'default-text-color'
All of the same callbacks are supported, exactly as before.
For reference, here is the complete defaults array:
$defaults = 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' => '', );
As you can see from the defaults array, this new implementation adds some new functionality: flexible headers. See Amy Hendrix’s excellent write-up for more information on this feature.
Ryan Hellyer 7:49 pm on April 6, 2012 Permalink |
Looks slick! Thanks for the run down on how it works Chip.
Emil Uzelac 9:28 pm on April 6, 2012 Permalink |
Bravo Chip!
Piet 2:28 am on April 7, 2012 Permalink |
Excellent information, thanks Chip!
Do you also know what exactly the function is of the last 3 arguments (‘wp-head-callback’ => ‘_custom_background_cb’, ‘admin-head-callback’ => ” and ‘admin-preview-callback’ => ”) of the custom-background?
The reason I ask is because I have been trying to add some text to the upload custom background screen, but haven’t figured out how to do that yet. Would be great if you can explain/show how that too.
Cheers,
Piet
Creative Media + 7:02 pm on April 7, 2012 Permalink |
Good question. I have been wondering about the same exact thing!
How can that be done? Thx much
Chip Bennett 6:11 pm on April 8, 2012 Permalink |
These callbacks *should* work the same as with the custom header feature. I’m hoping to have time to write a more in-depth tutorial later, going into full details. But for the time being, have a look at Twenty Eleven. The current version implements all three callbacks.
Rilwis 2:21 pm on April 7, 2012 Permalink |
Great! Thanks for letting us know about these cool updates.
How to add default custom background image WordPress 3.4? - WP TIPS 2:54 am on April 8, 2012 Permalink |
[...] The credits for the code above go to Chip Bennett who wrote extensively about the custom backgrounds and custom headers in this article. [...]
Flick 4:31 am on April 8, 2012 Permalink |
Looks like a great new feature! Am conscious of the fact that TwentyTen, for instance, uses the custom header feature – will it be updated for 3.4? Thanks
Chip Bennett 6:12 pm on April 8, 2012 Permalink |
I believe Twenty Eleven already has been updated (in trunk). I’m not sure about Twenty Ten. I would guess that it will be, but I really don’t know.
Flick 5:03 am on April 17, 2012 Permalink |
Thanks Chip
Michael 10:15 am on April 10, 2012 Permalink |
If I use the new methods in my themes this will not work in WP versions prior to 3.4 of course. is there an easy fallback? Or the other way around. How long can I still use the old methods? Any ideas?
Cheers
Michael
Chip Bennett 1:48 pm on April 10, 2012 Permalink |
We’ll discuss specifics as we start to formalize the guidelines revisions for the 3.4 update; however, some form of backward compatibility will certainly be allowed. Our current practice is: 1) no deprecated function calls, and 2) no backward compatibility beyond one prior major release. So, I imagine we’ll end up with Themes being required to use the new implementation, but also being able to provide back-compat for 3.3 users (via version check, or function_exists() check, etc.). I’ll have to go back to see how we handled the deprecation/change to contextual help in 3.3, since it is a nearly identical situation.
Michael Fields 4:37 pm on April 16, 2012 Permalink |
P2 version 1.4.0 has a neat way to deal with backward compatibility. You can browse the code here: http://themes.svn.wordpress.org/p2/1.4.0/inc/custom-header.php
WordPress Custom Headers Update for 3.4 | Jeremy Clark - TechNet.com 1:03 pm on April 11, 2012 Permalink |
[...] Make WordPress Themes If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to [...]
Northern-Web-Coders 9:17 am on April 14, 2012 Permalink |
[...] Das Theme unterstützt custom image header und custom background. Ab WordPress 3.4 sollen diese Funktionen geändert werden. Infos dazu gibts z.B. hier: Codex-Custom Headers oder Updating Custom Backgrounds and Custom Headers for WordPress 3.4 [...]
Silke 6:38 pm on April 16, 2012 Permalink |
Hi there, is there any css way to style that uploaded custom-background image? I found the WP.org text about body class, but I am not sure, if that is the answer and how to make ist work…
Thanks
Qamar 11:57 am on April 18, 2012 Permalink |
Thanks a lot for the update.
Dougal Campbell: Updating Custom Backgrounds and Custom Headers for WordPress 3.4 « Make WordPress Themes | WordPress Planet 10:01 am on April 23, 2012 Permalink |
[...] Updating Custom Backgrounds and Custom Headers for WordPress 3.4 « Make WordPress Themes [...]
winsorcm 4:28 am on June 5, 2012 Permalink |
Hi there. I have never touched the functions.php file but here is what i’d like to do:
Thank you!
What's new with WordPress 3.4 - Video 9:44 pm on June 7, 2012 Permalink |
[...] Chip Bennett explains the new themes API [...]
WordPress 3.4 RC2 - { EyesX } Mattias Tengblad Online 10:47 pm on June 7, 2012 Permalink |
[...] Chip Bennett – Summary of the API changes [...]
Maor Chasen 6:55 pm on June 13, 2012 Permalink |
This is an amazing improvement! Thanks for the short guide, Chip!
Michelle 8:55 pm on June 13, 2012 Permalink |
Thank you!! Amazing timing, I was just about to go figure out how to do this in 3.3.2 – yay WordPress developers and thank so much for the explanation!
WordPress 3.4 out and running | Anthalis Central 7:09 am on June 14, 2012 Permalink |
[...] to leverage the Theme Customizer in your own themes Flexible headers in WordPress 3.4 themes Updating Custom Backgrounds and Custom Headers for WordPress 3.4 Important Changes for WordPress 3.4 (on [...]
Richard 12:28 pm on June 14, 2012 Permalink |
Any idea why trying to show the default image like so doesn’t work?
echo ‘
default-image;
echo’” width=”100%” alt=”Header Image” />’;
Chip Bennett 12:41 pm on June 14, 2012 Permalink |
You might try asking on the theme-reviewers mail-list, or in this case, the wordpress.org support forums, for this type of question.
Richard 12:30 pm on June 14, 2012 Permalink |
Damn.
Bet this doesn’t work either.
get_custom_header()->default-image
Chip Bennett 9:53 pm on June 14, 2012 Permalink |
If I’m reading source correctly, this should work:
$custom_header_args = get_theme_support( ‘custom-header’ );
$defaultimage = $custom_header_args['default-image'];
Feature Breakdown of WordPress 3.4 "Green" | WPCandy 8:40 pm on June 14, 2012 Permalink |
[...] [...]
WordPress 3.4 Green is Good to Go « Lorelle on WordPress 9:26 pm on June 15, 2012 Permalink |
[...] on how these new features work. I also recommend a review of his WordPress Settings API Tutorial. Chip Bennett describes even more on how to update your WordPress Theme for custom backgrounds and [...]
WordPress 3.4 知っておくとあんしん・便利なリンク集 | 8bitodyssey.com 7:39 am on June 16, 2012 Permalink |
[...] Updating Custom Backgrounds and Custom Headers for WordPress 3.4 « Make WordPress Themes (カスタム背景とカスタムヘッダー実装の変更点) [...]
Die Neuigkeiten von WordPress 3.4 im Überblick | Elmastudio 10:43 am on June 18, 2012 Permalink |
[...] hilfreicher Artikel, um deine Header- und Background-Optionen upzudaten ist “Updating Custom Backgrounds and Custom Headers for WordPress 3.4″ von Chip Bennett. Eine Übersicht aller Optimierungen und Änderungen von WordPress 3.4 [...]
OffCourse2010 5:34 pm on June 19, 2012 Permalink |
Hi
For a theme I used the previous code which included some examples for the header that the user could choose from start. Is there a way to do something similar with backgrounds, to allow, for example to have a paper, wood, metal, etc texture to choose from?
Thank you
surowski 8:11 pm on February 3, 2013 Permalink |
hi mate, what about a transparent (or semi-transparent) background on one of the subpages
(not to change the main background, but only one of the sub-pages – to semi-transparent. that the main background would be visible) ? is there a solution?
Rob 7:35 pm on February 19, 2013 Permalink |
Hi and thanks for the great article. I just started with WordPress a few days ago (normly I am a Joomla guy) and I am sorting out how to add options to the theme I just created.
This is just a great option. But I would like to know, if it is possible to change only the background colors without offering the option to add a background image?
Thanks in advance for the support.
Best regards from Berlin (Germany),
Rob van Linda
Chip Bennett 4:22 pm on February 26, 2013 Permalink |
Rob,
This is certainly possible, but a Theme that did so would not be accepted for inclusion in the official Theme directory.