How to change WordPress default embed size using filters

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 Staff is a design blog about WordPress and web design. We usually 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. You can also subscribe to our mailing list. No spam, we promise.

9 Comments on How to change WordPress default embed size using filters

  1. 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?


  2. 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. 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. 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

  5. Nice and useful snippet, thanks!

  6. Will this work for all video sites?

    • 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.

7 Trackbacks & Pingbacks

  1. How to modify size of embedded videos - Broadys Web Design
  2. تغيير طول وعرض فيديو يوتيوب تلقائيا عند ادراجه في الموضوع
  3. How to modify size of embedded videos - WPInsite
  4. How To Change Default Embed Video Size With Filters
  5. Wordpress Youtube Videos automatische größe |
  6. How to change default embed size using filters – Tutorials | Shailan | MARVELTI
  7. Changing the default wordpress youtube embeded video dimensions | Technology & Programming

Leave a comment

Your email address will not be published.