WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] oEmbed now only has the width of 500px (7 posts)

  1. azn137
    Member
    Posted 1 year ago #

    After upgrading my WordPress to 3.5, all of my embedded videos are showing up with the width of 500px. How do I change it?

    There's no longer embed default size in Settings >> Media Settings.

  2. azn137
    Member
    Posted 1 year ago #

    For the time being, with WordPress 3.5, I've modified the function in /wp-includes/media.php to have the width of what I want:

    function wp_embed_defaults() {
    	if ( ! empty( $GLOBALS['content_width'] ) )
    		$width = (int) $GLOBALS['content_width'];
    
    	if ( empty( $width ) )
    		$width = 613;
    
    	$height = min( ceil( $width * 1.5 ), 1000 );
    
    	return apply_filters( 'embed_defaults', compact( 'width', 'height' ) );
    }
  3. Hi azn137,

    Best practice is to never hack core!

    The Embed size options were intentionally removed in 3.5. By default, if no content_width is defined, WordPress defaults to 500px. Maximum height is automatically calculated at 1 1/2 times the width.

    However, just because your theme hasn't set a content_width, it doesn't mean you can't.

    You can put something like this in your theme's functions.php file to change the default, where 500 represents the width in pixels:

    function my_content_width() {
    	global $content_width;
    	$content_width = 500;
    }
    add_action( 'template_redirect', 'my_content_width' );
  4. azn137
    Member
    Posted 1 year ago #

    Drew, IMO that's gonna cause a lot of issues, since some, if not most, themes don't already have the variable set.

  5. azn137
    Member
    Posted 1 year ago #

    Thanks for the code by the way. I guess I'd rather modify my theme file than core file.

  6. Drew, IMO that's gonna cause a lot of issues, since some, if not most, themes don't already have the variable set.

    Right. The point is to get theme authors to set a content_width in their themes.

    Also, I notice that the core function you hacked actually has a filter on the end of it. Filters are made for modifying output of core functions.

    Instead of the function above, you could just filter the core function directly with something like this:

    function filter_embed_sizes() {
    	return array( 'width' => 500, 'height' => 1000 );
    }
    add_filter( 'embed_defaults', 'filter_embed_sizes' );
  7. azn137
    Member
    Posted 1 year ago #

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic