Videos
The videos utility function provides a tool for generating HTML video tags with support for lazy loading and custom attributes.
Available Function
cbo_video($url, $options)
Generates an HTML video tag with various options.
Parameters:
$url: URL of the video.$options: Configuration options for the video tag.
Options:
echo: Display directly (true) or return HTML (false) (default: true).lazy: Enable lazy loading (default: true).lazyClass: CSS class for lazy loading (default: 'lazy').class: Additional CSS classes (default: '').type: MIME type of the video (default: 'video/mp4').attr: Additional HTML attributes (default: 'playsinline controls').
Return Value:
Returns the HTML video tag if echo is false, otherwise outputs it directly.
1// Basic usage2$video_url = 'https://example.com/video.mp4';3cbo_video($video_url);45// With custom options6cbo_video($video_url, [7'class' => 'featured-video',8'attr' => 'playsinline controls autoplay muted loop',9'lazy' => true10]);1112// Return HTML instead of echoing13$video_html = cbo_video($video_url, [14'echo' => false,15'class' => 'hero-video'16]);1718// Later use the HTML19echo '<div class="video-container">';20echo $video_html;21echo '</div>';
Usage Examples
Background Video
1// Create a background video2function display_background_video($video_url) {3echo '<div class="background-video-container">';4cbo_video($video_url, [5'attr' => 'playsinline autoplay muted loop',6'class' => 'background-video',7'lazy' => false // Background videos should load immediately8]);9echo '</div>';10}1112// Usage13display_background_video('https://example.com/background.mp4');
Video with Poster Image
1// Display a video with a poster image2function display_video_with_poster($video_url, $poster_url) {3echo '<div class="video-wrapper">';4cbo_video($video_url, [5'attr' => 'playsinline controls poster="' . esc_url($poster_url) . '"',6'class' => 'content-video'7]);8echo '</div>';9}1011// Usage12display_video_with_poster(13'https://example.com/video.mp4',14'https://example.com/poster.jpg'15);
ACF Video Field
1// Display a video from an ACF field2function display_acf_video($post_id = null) {3if (!$post_id) {4$post_id = get_the_ID();5}67$video = get_field('video', $post_id);89if (!$video) {10return;11}1213echo '<div class="featured-video-container">';14echo '<h3>' . get_field('video_title', $post_id) . '</h3>';1516cbo_video($video['url'], [17'class' => 'featured-video',18'attr' => 'playsinline controls'19]);2021if (get_field('video_caption', $post_id)) {22echo '<p class="video-caption">' . get_field('video_caption', $post_id) . '</p>';23}2425echo '</div>';26}2728// Usage29display_acf_video();
Best Practices
- Use lazy loading for videos that are not immediately visible to improve page load performance.
- Always include controls for user-interactive videos.
- Use muted and autoplay for background videos.
- Include a poster image to display before the video loads.
- Consider mobile users by using the playsinline attribute for iOS compatibility.