Change the default video URL in a post to shortcode format

admin2025-01-08  4

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.

Share Improve this question edited Apr 23, 2013 at 9:31 birgire 67.8k7 gold badges119 silver badges251 bronze badges asked Apr 23, 2013 at 7:58 MindZMindZ 113 bronze badges 3
  • Not sure I understand you correctly … do you mean Show shortcode without executing it? – fuxia Commented Apr 23, 2013 at 8:00
  • when click on the shortcode it should display the video. – MindZ Commented Apr 23, 2013 at 8:13
  • Youtube is already supported as part of the embed functionality along with various other popular video sites. – t31os Commented Apr 23, 2013 at 8:20
Add a comment  | 

2 Answers 2

Reset to default 1

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>';
}

Result

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736268947a1314.html

最新回复(0)