0

I'm using the code below to set the width and height of youtube embeded links

//////////////////////////////////////////////////////////////////
// Youtube shortcode
//////////////////////////////////////////////////////////////////
add_shortcode('youtube', 'shortcode_youtube');
function shortcode_youtube($atts) {
    $atts = shortcode_atts(
        array(
            'id' => '',
            'width' => 845,
            'height' => 500
        ), $atts);

        return '<div class="video-shortcode"><iframe title="YouTube video player" width="' . $atts['width'] . '" height="' . $atts['height'] . '" src="http://www.youtube.com/embed/' . $atts['id'] . '" frameborder="0" allowfullscreen></iframe></div>';
    }

For example, I will place this link --> https://youtu.be/YQHsXMglC9A , in the post and it automatically embeds.

The code above isn't rendering the width and height. I also tried the code below.

add_filter( 'embed_defaults', 'change_embed_size' );

function change_embed_size() {
// Adjust values
return array('width' => 800, 'height' => 500);
}

That didn't work either. Also that code seems like it targets all embeds on the site I want it to specifically target youtube.

Is there another option or way to make any of these work.

1
  • Youtube Embeds happen via core embeds and OEmbed, not a youtube shortcode. That's why your HTML does not change when you change your shortcode, because your shortcode is never used, and that's not how Youtube embeds work, or how to change their size
    – Tom J Nowell
    Commented Nov 13, 2021 at 20:15

1 Answer 1

0

Found a solution.

Adding this code below to the functions.php file of your theme will do the trick.

if ( ! isset( $content_width ) ) $content_width = YOUR WIDTH HERE;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.