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.

Please like & share: Staff 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.

You may also like...

16 Responses

  1. Nick Hiley says:

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

  5. Theo says:

    Nice and useful snippet, thanks!

  6. Renji says:

    Will this work for all video sites?

  1. February 11, 2011

    […] return $ embed_size; } add_filter('embed_defaults', 'mycustom_embed_defaults'); Credits goes to Shailan. Thanks for the cool tip!GD Star Ratingloading…Share this Related posts:How to make DHTML […]

  2. February 11, 2011

    […] هذا الكود ينبه الى ان الفيديو سيظهر بطريقة تناسبية بمعنى ان […]

  3. February 28, 2011

    […] return $ embed_size; } add_filter('embed_defaults', 'mycustom_embed_defaults'); Credits goes to Shailan. Thanks for the cool tip!VN:F [1.9.7_1111]please wait…Rating: 0.0/5 (0 votes cast)Share this […]

  4. March 8, 2011

    […] new size } add_filter('embed_defaults', 'mycustom_embed_defaults');Thanks to Shailan for this hack.You Might Like Theses Posts:Video Of Beyonce Painted By Kyle Lambert Using iPad […]

  5. May 11, 2011

    […] goes to Shailan. Thanks for the cool tip! Dieser Beitrag wurde unter Allgemein, WordPress abgelegt und mit […]

  6. December 5, 2011

    […] How to change default embed size using filters – Tutorials | Shailan. […]

  7. November 19, 2013

    […] dimensions in Settings > Media, but that feature has been deprecated. Another person blogging at suggested adding a […]

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>