has_custom_header()
Checks if an image is set for the theme header, or if a video is set and available for display on the current request page.
The function will work (return true) if an image is set for the header in the theme settings (customizer). Or if a video is set and this video should be displayed on the current page (usually videos are shown only on the homepage).
The ability to set an image or video for the header is activated via add_theme_support( 'custom-header' );
No Hooks.
Returns
true|false. True - if the header element is set. False - if not.
Usage
if( has_custom_header() ){
// video or image is set for the theme header
}
Examples
#1 Check if there is a image or a video for the site Header
If there is, we will display the HTML code:
if( has_custom_header() ){
the_custom_header_markup();
}
For a image it will output HTML:
<div id="wp-custom-header" class="wp-custom-header"> <img src="http://example.com/wp-content/uploads/2016/05/image.jpg" width="954" height="1300" alt="Test Site srcset="http://example.com/wp-content/uploads/2016/05/image.jpg 954w, http://example.com/wp-content/uploads/2016/05/image-220x300.jpg 220w, http://example.com/wp-content/uploads/2016/05/image-768x1047.jpg 768w, http://example.com/wp-content/uploads/2016/05/image-751x1024.jpg 751w" sizes="(max-width: 954px) 100vw, 954px" /> </div>
The following HTML will be output for the video. And also in the footer will be connected scripts. HTML video is created by the script and replaces the HTML images.
<div id="wp-custom-header" class="wp-custom-header"> <video id="wp-custom-header-video" autoplay="" loop="" width="954" height="1300" src="http://example.com/wp-content/uploads/2017/01/polina.mp4"></video> <button type="button" id="wp-custom-header-video-button" class="wp-custom-header-video-button wp-custom-header-video-play">Pause</button> </div>
Scripts in the footer:
<script type='text/javascript' src='http://example.com/wp-includes/js/wp-custom-header.min.js'></script> <script type='text/javascript' src='http://example.com/wp-includes/js/mediaelement/mediaelement-and-player.min.js'></script> <script type='text/javascript' src='http://example.com/wp-includes/js/mediaelement/wp-mediaelement.min.js'></script>
Changelog
| Since 4.7.0 | Introduced. |
has_custom_header() has custom header code WP 6.9
function has_custom_header() {
if ( has_header_image() || ( has_header_video() && is_header_video_active() ) ) {
return true;
}
return false;
}