If you’re a theme developer and like to use WordPress’ add_custom_background() function in your themes, you’ve probably noticed a small problem before with the way this is handled. If you haven’t noticed, you may be in for a surprise after reading this tutorial and testing your theme.
The “bug” occurs when you define a custom background image in your theme’s style.css file but also add support for WordPress’ native custom background feature. If the user just wants to use a background color (but not an image), your theme’s background in style.css will hide the the user’s selected background color.
This tutorial will walk you through the issue and how to fix it for your theme.
Spotting the background issue
Let’s assume you have some CSS defined in your theme that adds a custom, repeating background image to the body element in your theme. Your code would look something like the following.
body {
background: #f7f7f7 url(images/bg-body.png) repeat 0 0;
}
Your theme’s display would look something like the following screenshot.

So far, everything should be in working order for your theme.
Now, let’s suppose you added a call to add_custom_background() in your theme’s functions.php file. This feature will allow theme users to add a custom background color and/or background image. The background image functionality should work perfectly with your theme. The problem is when a user selects only a custom background color as shown in the next screenshot.

When the user views their site, they will not see their selected background color. They will still see your theme’s background image. This is because WordPress will only add CSS for background-color rather than background. Thus, the background image defined in style.css hides the background color.
There are pros and cons to WordPress handling the custom background feature this way. And, the workaround for this problem is pretty easy.
The custom background fix
Most theme developers simply drop the following line of code in their theme’s functions.php file.
add_custom_background();
Many of them don’t know about the parameters for the add_custom_background() function:
add_custom_background( $header_callback, $admin_header_callback, $admin_image_div_callback );
The parameter we’re concerned about for this tutorial is the $header_callback parameter. It allows you to define a custom function that will be used to display the <style> output in the theme header, overwriting what WordPress would normally output.
To use this functionality, you ned to replace your call to add_custom_background() in your theme’s functions.php file with the following line of code.
add_custom_background( 'my_custom_background_callback' );
Then you need to add your custom callback function to functions.php. The following code will do a couple of things:
- Check if there’s a background image and default to the WordPress output if one is found.
- Use
backgroundinstead ofbackground-colorwhen a user defines a custom background color but not a custom background image.
function my_custom_background_callback() {
/* Get the background image. */
$image = get_background_image();
/* If there's an image, just call the normal WordPress callback. We won't do anything here. */
if ( !empty( $image ) ) {
_custom_background_cb();
return;
}
/* Get the background color. */
$color = get_background_color();
/* If no background color, return. */
if ( empty( $color ) )
return;
/* Use 'background' instead of 'background-color'. */
$style = "background: #{$color};";
?>
<style type="text/css">body { <?php echo trim( $style ); ?> }</style>
<?php
}
Fix those backgrounds
I can’t tell you how many times I’ve had to point out to theme developers when reviewing their themes that the user-selected background color is hidden by the theme’s background image. The above code will solve this issue.
There are other methods for doing this, but I’ve found that this method allows for a lot of flexibility and puts me, the theme developer, back in control of the output.
Exactly what I needed! Thanks!
You’re welcome.
Really useful tip!
Unfortunately I realized it too late. In my themes I used to create my own custom function and everything worked great, but the last time I said “hei, why add unnecessary code when there is already the default function of WordPress, which does exactly the same thing??” How wrong I was …
Unfortunately neither I nor the reviewer have noticed this problem in the theme before it was released.
However, as well as I do not agree the logic with which the function of WP handles the background – as you said, there’s pros and cons – , I think it lacks of the “position-y” option, which could be useful for fixed backgrounds.
Yeah, I used to do the same thing all the time. So, every time I want to add some new functionality to a theme/plugin, I ask myself, “Does WordPress already handle this?” Then, I begin scouring the WordPress source code for answers.
Hi Justin,
thanks for this useful tip. I haven’t noticed this “bug” until today
All of my published themes use the custom background function and have luckily no default background image, only color.
But I have a new theme ready to publish with default background image. Good to know I can fix this bug now before it goes online.
Cheers,
Thomas
I first came across it when performing theme reviews for WordPress.org, so a lot of people are unaware of the issue.
Let me know how the code works out for you.
The code works fine. Thanks again for this cool code snippet.
A nice tutorial. Actually, I don’t often use custom background in my themes as I always fixed the background by CSS, but this’s really needed when users need to change the background themselves. Thanks.
Rilwis.
In the past, I didn’t much like the custom background feature, but it’s starting to grow on me. It’s such a simple thing to add to give your users just a little extra flexibility.
Nice tut! I have had my thoughts about it, especially when it comes to make use of an option panels AND the ability to “add_custom_background”. This fix opens more flexibility, and besides that – i learned something new today
Tnx, Lillan
Yep, there’s tons of flexibility when you roll your own background callback function. You can even add in additional style rules based on whether a background color and/or image is set.
Justin, you are a genius! I didn’t even know I needed to fix this. Thanks!
Nice Tip!!! Thanks
hi , my wordpress don’t display image background
file function.php
style.css
color is ok, but something wrong is with image , great thanks and regard
WordPress 3.4 is here, and it seems that the problem still exists. How would I go about solving it, now that the function add_custom_background is deprecated?
@pikolo and Alex
I added this in my functions.php:
and this in my start body tag in my header.php:
It did the trick also in WP 3.5
hope it helps
Thanks
hey i added both add_custom_background() and its new replacement but nothing seems to work, i tried your example too..i feel so stupid right now.. please help.. shoild i add something in header.php or index.php..?