Using WordPress ‘header_image()’ PHP function

The header_image() WordPress PHP function displays the URL of the current theme’s header image.

Usage

To display the header image URL, simply use the function like this:

echo header_image();

Parameters

This function has no parameters.

More information

See WordPress Developer Resources: header_image()

This function is related to the has_header_image() and get_header_image() functions.

Examples

Display the header image in an img tag

To display the header image in an <img> tag:

echo '<img src="' . header_image() . '" alt="Header Image">';

Set a background image for a div

To set the header image as a background image for a <div> element:

echo '<div style="background-image: url(' . header_image() . ');">Content</div>';

Display a default image if no header image is set

To display a default image if no header image is set:

if (has_header_image()) {
    echo '<img src="' . header_image() . '" alt="Header Image">';
} else {
    echo '<img src="path/to/default/image.jpg" alt="Default Image">';
}

To create a link to the homepage using the header image:

echo '<a href="' . esc_url(home_url('/')) . '"><img src="' . header_image() . '" alt="Header Image"></a>';

Add a CSS class to the header image

To add a CSS class to the header image:

echo '<img src="' . header_image() . '" alt="Header Image" class="custom-header-image">';