I want to add video from youtube/vimeo to my post.Whenever i add a video to my post ,there will display a link like in my admin(Add new post ),and in the front end it will display the video.But the problem is, i want to show the video like a shortcode example [video url=””][/video]in the place of original video.That is show the shortcode like above instead of the video in front end.How can it possible.Please help me.
I want to add video from youtube/vimeo to my post.Whenever i add a video to my post ,there will display a link like https://www.youtube.com/watch?v=dsUXAEzaC3Q in my admin(Add new post ),and in the front end it will display the video.But the problem is, i want to show the video like a shortcode example [video url=”https://www.youtube.com/watch?v=dsUXAEzaC3Q”][/video]in the place of original video.That is show the shortcode like above instead of the video in front end.How can it possible.Please help me.
Add the following code at the end of the functions.php
file and you have to replace the built-in shortcode.
function video_shortcode($atts) {
// Extract the URL from the shortcode attributes
$url = isset($atts['url']) ? esc_url($atts['url']) : '';
// Check if a valid URL is provided
if ($url) {
// Extract the video ID from the YouTube URL
$video_id = '';
parse_str(parse_url($url, PHP_URL_QUERY), $query_params);
if (isset($query_params['v'])) {
$video_id = $query_params['v'];
}
// If video ID found, generate the iframe code for embedding YouTube video
if ($video_id) {
$iframe_code = '<iframe width="560" height="315" src="https://www.youtube.com/embed/' . $video_id . '" frameborder="0" allowfullscreen></iframe>';
return $iframe_code;
}
}
// If no valid URL is provided, return an empty string
return '';
}
add_shortcode('custom_video', 'video_shortcode');
To render the shortcode output with a clickable link while it still looks like a shortcode, you have to replace the built-in shortcode:
add_shortcode( 'video', 'wpse_96840_literal_video' );
function wpse_96840_literal_video( $atts )
{
$url = $atts['url'];
return '<code>'
. sprintf( '[video url="<a href=\"%1$s\">%1$s</a>"][/video]', $url )
. '</code>';
}