The get_custom_header() WordPress PHP function retrieves the header image data for a custom header.
Usage
$custom_header = get_custom_header();
Parameters
- None
More information
See WordPress Developer Resources: get_custom_header()
Examples
Display the header image URL
To display the header image URL, use the following code:
$custom_header = get_custom_header(); echo esc_url($custom_header->url);
Display header image with custom size
To display the header image with a custom size, use the following code:
$custom_header = get_custom_header(); if (!empty($custom_header->attachment_id)) { $image = wp_get_attachment_image_url($custom_header->attachment_id, 'custom-size'); echo esc_url($image); }
Display header image with alt text
To display the header image with alt text, use the following code:
$custom_header = get_custom_header(); echo '<img src="' . esc_url($custom_header->url) . '" alt="' . esc_attr(get_bloginfo('name')) . '">';
Add header image as a background
To add the header image as a background to a div, use the following code:
$custom_header = get_custom_header(); echo '<div style="background-image: url(' . esc_url($custom_header->url) . ');"></div>';
Check if a custom header is set
To check if a custom header is set and display it, use the following code:
$custom_header = get_custom_header(); if (!empty($custom_header->url)) { echo '<img src="' . esc_url($custom_header->url) . '" alt="' . esc_attr(get_bloginfo('name')) . '">'; }