Using WordPress ‘add_custom_background()’ PHP function

The add_custom_background() WordPress PHP function is used to add callbacks for background image display. This function aids in creating custom background display on your WordPress website.

Usage

To use this function, simply call it in your theme’s functions.php file, providing optional callback parameters as needed:

add_custom_background($wp_head_callback, $admin_head_callback, $admin_preview_callback);

Parameters

  • $wp_head_callback (callable, optional): This parameter is used to call on the ‘wp_head’ action. Default value is ”.
  • $admin_head_callback (callable, optional): Used to call on the custom background administration screen. Default value is ”.
  • $admin_preview_callback (callable, optional): Outputs a custom background image div on the custom background administration screen. This parameter is optional. Default value is ”.

More information

See WordPress Developer Resources: add_custom_background()

This function has been deprecated since version 3.4.0. It’s recommended to use add_theme_support() for adding custom background support.

Examples

Default Usage

// Add support for custom background
add_custom_background();

In this example, the function will add the default support for custom background in your WordPress theme.

Using $wp_head_callback

// Define a callback function
function my_custom_background() {
    echo '<style>body { background-color: #fafafa; }</style>';
}

// Add custom background with a callback
add_custom_background('my_custom_background');

This example demonstrates the usage of the $wp_head_callback parameter. The ‘my_custom_background’ function is defined, which will be called to output some CSS to set a custom background color.

Using $admin_head_callback

// Define a callback function
function admin_custom_background() {
    echo '<style>.wrap { background-color: #fafafa; }</style>';
}

// Add custom background with a callback
add_custom_background('', 'admin_custom_background');

In this example, the $admin_head_callback parameter is used. The ‘admin_custom_background’ function is defined and used to change the background color of the administration screen.

Using $admin_preview_callback

// Define a callback function
function preview_custom_background() {
    echo '<div style="height: 100px; width: 100px; background-color: #fafafa;"></div>';
}

// Add custom background with a callback
add_custom_background('', '', 'preview_custom_background');

This example uses the $admin_preview_callback parameter. The ‘preview_custom_background’ function is defined and used to output a preview of the custom background on the administration screen.

Using All Parameters

// Define callback functions
function my_custom_background() {
    echo '<style>body { background-color: #fafafa; }</style>';
}

function admin_custom_background() {
    echo '<style>.wrap { background-color: #fafafa; }</style>';
}

function preview_custom_background() {
    echo '<div style="height: 100px; width: 100px; background-color: #fafafa;"></div>';
}

// Add custom background with all callbacks
add_custom_background('my_custom_background', 'admin_custom_background', 'preview_custom_background');

This example demonstrates usage of all parameters. Each parameter is defined with a callback function. The ‘my_custom_background’ function sets the custom background color, ‘admin_custom_background’ changes the background color of the administration screen, and