questions

Shortcode for youtube videos wordpress

Rate this post

I want to add a shortcode to add a youtube video to my website. I want to block the content with cookiebot.

Here is the code how it would look if I add a youtube video without any shortcode:

<div class="cookieconsent-optout-marketing">
      Please 
    <a href="javascript:Cookiebot.renew()">accept marketing-cookies</a> to watch this video.
    
</div>
<iframe data-src="https://www.youtube.com/embed/xxxxxxxxxxx" data-cookieconsent="marketing" frameborder="0" allowfullscreen></iframe>

So it’s difficult for me how to use the shortcode. I only know how to add a new shortcode. Here is my shortcode until now:

<?php

function youtube_function()
{

    $output = '
           <div class="cookieconsent-optout-marketing">
             Please <a href="javascript:Cookiebot.renew()">accept marketing-cookies</a> to watch this video.
           </div>
           
           <iframe data-src="https://www.youtube.com/embed/xxxxxxxxxxx" data-cookieconsent="marketing" frameborder="0" allowfullscreen></iframe>';

    return $output;
}

add_shortcode('ytvideo', 'youtube_function');

?>

Now I want to replace the link or maybe the xxxxxxxxxx section. I hope someone want to help me 🙂

 

✔️Solution:

 <?php

function youtube_function($atts) {

    extract(shortcode_atts( array(
        'video' => '',
    ), $atts ));
    
    $output = '
    <div class="cookieconsent-optout-marketing">
    Please <a href="javascript:Cookiebot.renew()">accept marketing-cookies</a> to watch this video.
    </div>

    <iframe data-src="https://www.youtube.com/embed/' . $video .'" data-cookieconsent="marketing" frameborder="0" allowfullscreen></iframe>';

    return $output;
}



add_shortcode('ytvideo', 'youtube_function');

?>

Use the shortcode in this way: [ytvideo video=”id_of_video_xxxx”]

Leave a Reply

Your email address will not be published.

Back to top button