Using WordPress ‘background_color()’ PHP function

The background_color() WordPress PHP function displays the background color value of the current theme.

Usage

This function is typically used to set the background color of a div element in your theme. Here’s a custom example:

<div id="myDiv" style="background-color: #<?php background_color(); ?>">
  This is my content.
</div>

In this example, the output will be a div with the theme’s background color.

Parameters

The background_color() function does not have any parameters.

More information

See WordPress Developer Resources: background_color()

This function is a part of the WordPress core and is not deprecated. It is available in all versions of WordPress.

Examples

Example 1 – Applying theme background color to a div

In this example, the background_color() function is used to apply the theme’s background color to a div.

<div id="myDiv" style="background-color: #<?php background_color(); ?>">
  This is my content.
</div>

Example 2 – Applying theme background color to a body tag

Here, we use the background_color() function to set the theme’s background color to the body tag.

<body style="background-color: #<?php background_color(); ?>">
  This is my content.
</body>

Example 3 – Using in CSS

In this example, we use the background_color() function inside a CSS style block.

<style>
  .myClass {
    background-color: #<?php background_color(); ?>;
  }
</style>

Example 4 – Applying to a button

In this example, the function is used to apply the theme’s background color to a button.

<button style="background-color: #<?php background_color(); ?>">
  Click me
</button>

Example 5 – Applying to text color

We can also use the background_color() function to set the text color instead of the background color.

<p style="color: #<?php background_color(); ?>">
  This is my content.
</p>