$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Changing the default object embed sizes?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Changing the default object embed sizes?

matteradmin9PV0评论

I was using the code below to set the default size for embeded objects in my posts. It worked before for me but now it no longer works.

 add_filter( 'embed_defaults', 'bigger_embed_size' );

 function bigger_embed_size()
 { 
 return array( 'width' => 935, 'height' => 525 );
 }

I also used this css so that when viewed from mobile devices the width of the embeded object would be so tall.

embed,
iframe,
object,
script,
video {
margin-bottom: 1.6em;
max-width: 100% !important;
vertical-align: middle !important;
min-height:250px;
}

None of these options are working anymore does anyone have a solution.

I was using the code below to set the default size for embeded objects in my posts. It worked before for me but now it no longer works.

 add_filter( 'embed_defaults', 'bigger_embed_size' );

 function bigger_embed_size()
 { 
 return array( 'width' => 935, 'height' => 525 );
 }

I also used this css so that when viewed from mobile devices the width of the embeded object would be so tall.

embed,
iframe,
object,
script,
video {
margin-bottom: 1.6em;
max-width: 100% !important;
vertical-align: middle !important;
min-height:250px;
}

None of these options are working anymore does anyone have a solution.

Share Improve this question asked Mar 14, 2017 at 8:10 Terrell AndersonTerrell Anderson 657 bronze badges 7
  • Have you added any plugins that might override them? Perhaps upping the priority might help. add_filter( 'embed_defaults', 'bigger_embed_size', 100 ); – Ben Casey Commented Mar 14, 2017 at 8:24
  • I don't think i've added any plugin that would effect media sizes – Terrell Anderson Commented Mar 14, 2017 at 8:26
  • and upping the priority didn't help :( – Terrell Anderson Commented Mar 14, 2017 at 8:27
  • silly question, but sometimes its those that answer the problem, is the add_filter actually running? try die('HERE') right before it and ensure that "HERE" is the only thing printed to the screen. Otherwise try looking for PHP errors. – Ben Casey Commented Mar 14, 2017 at 8:29
  • is it public so we can see the issue? – Ben Casey Commented Mar 14, 2017 at 8:29
 |  Show 2 more comments

1 Answer 1

Reset to default 0

This is what I crafted to override the defaults, I set a default media width, choose an aspect ratio (16:9) and then replace the video shortcode output.

$defaultMediaWidth = 960;

if ( ! isset( $content_width ) ) {
  $content_width = $defaultMediaWidth;
}
add_filter( 'embed_defaults', 'bigger_embed_size');

function bigger_embed_size()
{
  global $content_width;
  $width = $content_width;
  // aspect ratio 16:9
  $r1 = 16;
  $r2 = 9;
  $r = $width / $r1;
  $height = $r * $r2;

  return array( 'width' => $width, 'height' => $height );
}

function EMP_video_shortcode_override(
  $output = '',
  $atts = [],
  $video = '',
  $post_id = 0,
  $library = ''
){
  $defaults = wp_embed_defaults();
  $searchfor = [
    'width="' . $atts['width'] . '"',
    'width: ' . $atts['width'] . 'px;',
    'height="' . $atts['height'] . '"'
  ];
  $replaceto = [
    'width="' . $defaults['width'] . '"',
    'width:' . $defaults['width'] . 'px;',
    'height="' . $defaults['height'] . '"'
  ];
  return str_ireplace( $searchfor, $replaceto, $output );
}
add_filter('wp_video_shortcode', 'EMP_video_shortcode_override', 10 , 5);

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far