Last active
November 12, 2024 14:38
-
-
Save andrewinsidelazarev/70f581460419813773aeb848f4ad8c62 to your computer and use it in GitHub Desktop.
Shortcode for Youtube video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function youtube_embed_shortcode($atts) { | |
// Extract the video ID from the shortcode parameters | |
$atts = shortcode_atts(array( | |
'id' => '' | |
), $atts); | |
// Check if the video ID is provided | |
if (empty($atts['id'])) { | |
return '<p>Please provide a video ID.</p>'; | |
} | |
// Construct the thumbnail URL in WebP format | |
$thumbnail_url = 'https://i.ytimg.com/vi_webp/' . esc_attr($atts['id']) . '/maxresdefault.webp'; | |
// Generate HTML with a thumbnail, SVG play button overlay, and video iframe | |
$output = '<div style="position: relative; width: 100%; padding-top: 56.25%;">'; | |
$output .= '<img src="' . esc_url($thumbnail_url) . '" alt="Video Thumbnail" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; cursor: pointer;" onclick="this.style.display=\'none\'; this.nextElementSibling.style.display=\'none\'; this.nextElementSibling.nextElementSibling.src=\'https://www.youtube.com/embed/' . esc_attr($atts['id']) . '?autoplay=1\'; this.nextElementSibling.nextElementSibling.style.display=\'block\';">'; | |
// Inline SVG play button | |
$output .= '<div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); cursor: pointer; z-index: 1;" onclick="this.previousElementSibling.click();">'; | |
$output .= '<svg width="68" height="48" viewBox="0 0 68 48" xmlns="http://www.w3.org/2000/svg"> | |
<path d="M66.52 7.68C65.76 5.23 63.97 3.44 61.52 2.68C57.68 1.48 34 1.48 34 1.48C34 1.48 10.32 1.48 6.48 2.68C4.03 3.44 2.24 5.23 1.48 7.68C0.28 11.52 0.28 24 0.28 24C0.28 24 0.28 36.48 1.48 40.32C2.24 42.77 4.03 44.56 6.48 45.32C10.32 46.52 34 46.52 34 46.52C34 46.52 57.68 46.52 61.52 45.32C63.97 44.56 65.76 42.77 66.52 40.32C67.72 36.48 67.72 24 67.72 24C67.72 24 67.72 11.52 66.52 7.68Z" fill="red"/> | |
<path d="M45 24L27 14V34L45 24Z" fill="white"/> | |
</svg>'; | |
$output .= '</div>'; | |
$output .= '<iframe src="" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen loading="lazy" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: none;"></iframe>'; | |
$output .= '</div>'; | |
return $output; | |
} | |
add_shortcode('youtube_embed', 'youtube_embed_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Решение проблемы с переходом в полноэкранный режим:
`
function youtube_embed_shortcode($atts) {
$atts = shortcode_atts(array(
'id' => '',
'sd' => false
), $atts);
}
add_shortcode('youtube_embed', 'youtube_embed_shortcode');
`