Using WordPress ‘get_background_color()’ PHP function

The get_background_color() WordPress PHP function retrieves the value for the custom background color of the current theme.

Usage

$background_color = get_background_color();
echo 'Current background color for your theme: #' . $background_color;

Parameters

  • None

More information

See WordPress Developer Resources: get_background_color()

Examples

Display the current background color

// Get the background color
$background_color = get_background_color();

// Display the background color
echo 'Current background color for your theme: #' . $background_color;

Set the body background color using the retrieved color

// Get the background color
$background_color = get_background_color();

// Set the body background color
echo '<body style="background-color:#' . $background_color . ';">';

Add a CSS class with the background color

// Get the background color
$background_color = get_background_color();

// Add a CSS class with the background color
echo '<style>.custom-bg-color { background-color: #' . $background_color . '; }</style>';

Display background color in an HTML element

// Get the background color
$background_color = get_background_color();

// Display the background color in an HTML element
echo '<div style="background-color:#' . $background_color . ';">Content with custom background color</div>';

Create a function to apply the background color to an element

function apply_custom_bg_color($element) {
    // Get the background color
    $background_color = get_background_color();

    // Return the element with the custom background color
    return '<' . $element . ' style="background-color:#' . $background_color . ';">';
}

// Use the function to apply the background color to an element
echo apply_custom_bg_color('div');