has_header_video()WP 4.7.0

Checks if a video is uploaded for the header in the theme settings (customizer).

The ability to add a header video was introduced in version 4.7. It is activated using add_theme_support( 'custom-header' );

This function only checks for the presence of an uploaded video, but does not check if this video should be displayed on the viewed page. By default, the header video is shown only on the homepage, while on other pages, the header image is displayed. This is done for obvious reasons: there is no need to load a video in the header on every page of the site, because such a video is primarily created for the impression of the site and negatively affects the page loading speed. And it will annoy users if it appears everywhere...

The conditional tag is_header_video_active() is responsible for which page the video is displayed on. It makes sense to use this function in conjunction with it:

## check if there is a header video and it should be displayed on the current page.
if( has_header_video() && is_header_video_active() ){
	// get the video link
	$videourl = get_header_video_url();
}

An alternative that immediately retrieves the video URL. It's more convenient when you need to get the video URL right away, rather than just checking:

$url = get_header_video_url();
if( $url ){
	echo $url;
}

Use has_header_image() when you need to perform the same check, but only for the header image.

Use has_custom_header() when you need to check for the presence of either a video or an image for the header at the same time.

1 time — 0.000058 sec (very fast) | 50000 times — 0.70 sec (very fast) | PHP 7.1.1, WP 4.7.1

No Hooks.

Returns

true|false. Whether there is a video or not.

Usage

if( has_header_video() ){
	// video is uploaded
}

Examples

0

#1 Display the header video

Check if the header video is there and if it should be displayed on the current page. If it's OK, we show the player with the video playing.

// check if there is a header video and it should show on the current page.
if( has_header_video() && is_header_video_active() ){
	the_custom_header_markup();
}

As a result, the HTML code of the video player will appear in place of this code, and the necessary scripts for the player will be connected to the page (to footer):

<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>

Notes

Changelog

Since 4.7.0 Introduced.

has_header_video() code WP 6.8.1

function has_header_video() {
	return (bool) get_header_video_url();
}