Using WordPress ‘has_custom_header()’ PHP function

The has_custom_header() WordPress PHP function checks if a custom header is set for the current theme.

Usage

has_custom_header()

Custom Example:

if (has_custom_header()) {
    // Display custom header
} else {
    // Display default header
}

Parameters

  • No parameters are required for this function.

More information

See WordPress Developer Resources: has_custom_header()

Examples

Display custom header image

Check if a custom header is set, and display it. Otherwise, display the default header image.

if (has_custom_header()) {
    echo '<img src="' . get_header_image() . '" alt="Custom Header Image">';
} else {
    echo '<img src="/path/to/default/header.jpg" alt="Default Header Image">';
}

Custom header with a fallback

Check if a custom header is set, and display it. If not, display a fallback image.

$header_image = has_custom_header() ? get_header_image() : '/path/to/fallback/header.jpg';
echo '<img src="' . $header_image . '" alt="Header Image">';

Add a CSS class to the header based on the custom header status

Add a specific CSS class to the header if a custom header is set.

$header_class = has_custom_header() ? 'custom-header' : 'default-header';
echo '<header class="' . $header_class . '">';

Custom header with a background color

Display a custom header with a background color if it is set, otherwise display a default background color.

$background_color = has_custom_header() ? 'style="background-color: #123456;"' : 'style="background-color: #abcdef;"';
echo '<header ' . $background_color . '>';

Hide navigation menu if custom header is set

Hide the navigation menu if a custom header is set.

if (!has_custom_header()) {
    wp_nav_menu(array('theme_location' => 'primary'));
}