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(
            '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.


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;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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