wordpress-video-embed-size

How to change WordPress default embed size using filters

on February 2, 2011 | 4,030 views

Embedding videos from video sites to your posts is a very simple task in WordPress. You can easily embed videos just pasting URL of the video to your posts. It automatically displays the video depending on your media settings. What if you wanted a different video size in single posts?

Then you will need to use a filter to change embed default dimensions depending on context. In this post, I will show you how to simply change WordPress default embed size for single posts. You can expand this trick to use with other contexts.

Simply open functions.php in your theme folder to create our filter.

Paste the following code:

function mycustom_embed_defaults($embed_size){
	if( is_single() ){ // If displaying a single post
		$embed_size['width'] = 586; // Adjust values to your needs
		$embed_size['height'] = 500; 
	}
	
	return $embed_size; // Return new size
}

add_filter('embed_defaults', 'mycustom_embed_defaults'); 

Save it and then visit your post page. It will display automatic embeds using new size you define on your single post pages. For home page wordpress will use default size, but in single post view,
WordPress will use default embed size you defined in the functions.php file.

Please note that, auto-embed embeds videos proportionally. So it will use either width or height for limiting video size. Output may not fit exact dimensions you declared here. I suggest using height a large value. This way you can be sure it will always use width constraint.

I hope you found this useful.

About Shailan.com Staff

Shailan.com is a design blog about WordPress and web design. We regularly write about wordpress, plugins, themes, web design resources, php, css, jquery, adsense and more.. If you want to get notified about our posts, be sure to subscribe to our feed or follow us on facebook.

Share Your Thoughts

Your email address will not be published.


16 thoughts on “How to change WordPress default embed size using filters”

  1. Nick Hiley says:

    Hey,
    Thanks for this snippet! In my function.php file, I’ve allowed video embeds in the excerpt but this snippet doesn’t affect the video in the except. What code would I need to add to do this globally?

    Thanks

  2. Pat says:

    PS: I find so hard to believe WP wouldn’t make this a standard option or how there are no plugins out there (plenty of video inserts) but nothing to just fix this simple thing.

  3. Pat says:

    Thank you :) NO error now when moved to the theme’s function.php. copy exact code and just change size… but I’m sorry to report it gave no error but didn’t work. Native WP embed size remains the same. I also set the Media setting sin the WP to match the max width just in case, no difference :(

  4. Pat says:

    Sorry to report it doesn’t work. copy/paste exact code a get error:
    Fatal error: Call to undefined function add_filter() in /wp-includes/functions.php on line 9. Line 9 being return $embed_size; // Return new size

    1. Matt Say says:

      You gotta put that code to functions.php in your theme folder not in wp-includes.

  5. Theo says:

    Nice and useful snippet, thanks!

  6. Renji says:

    Will this work for all video sites?

    1. shailan says:

      If the video supports oEmbed, WordPress automatically embed video using the link. If it doesn’t support oEmbed, you need to use object/embed code instead. This trick works for auto-embed only. Most common video sites like youtube, vimeo, dailymotion support oEmbed now.

      1. Renji says:

        Okay…thanks Shailan! :)