is_header_video_active()
Defines whether video can be displayed in the “custom‑header” area on the current page. Conditional tag.
This function is responsible for checking on which page to show the header video if it exists. Such video is by default shown only on the homepage of the site see is_front_page().
How it works (step by step):
-
The function first checks if the theme supports video in the header through get_theme_support( 'custom-header', 'video' ).
Support is enabled in the theme's functions.php through add_theme_support():
add_theme_support( 'custom-header', [ 'video'=>true ] );
-
Then it gets the callback
video-active-callback. If it exists, it is executed to determine if the video in the header is active; if it does not exist, it is assumed that the video can be shown, but only on the homepage. Callback:$video_active_cb = get_theme_support( 'custom-header', 'video-active-callback' );
- Before returning, the result is filtered through is_header_video_active.
This function does not check if the header video is set; it is responsible for checking whether it should be displayed.
Use has_header_video() to check if the header video is set.
Changing the check
This function is a wrapper for the function specified in the video-active-callback parameter when registering support add_theme_support('custom-header'). Suppose, when registering the header video, we specified the parameters like this:
add_theme_support( 'custom-header', [ 'video' => true, 'video-active-callback' => 'is_home', ] );
Then the operation of this function will be passed to is_home() and whatever it returns will be returned by this function.
However, this behavior can be overridden through the hook is_header_video_active. Through this hook, we can redefine the check and what this function returns. For example:
add_filter( 'is_header_video_active', 'my_is_header_video_active' );
function my_is_header_video_active( $show_video ){
// Allow header video to be shown only on the 'about' page
$show_video = is_page( 'about' );
return $show_video;
}
And now the function will always return true on the /about page and false on all other pages.
Hooks from the function
Returns
true|false. true - if the video should be shown on the current page. false - otherwise.
Usage
if( has_header_video() && is_header_video_active() ){
// ...
}
Examples
#1 Let's 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();
// url to video - get_header_video_url()
}
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 (at the end):
<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. |