Using WordPress ‘oembed_default_width’ PHP filter

The oembed_default_width WordPress PHP filter allows you to modify the maximum allowed width for oEmbed content. By default, the maximum width is set to 600 pixels.

Usage

add_filter('oembed_default_width', 'your_custom_function');
function your_custom_function($maxwidth) {
  // Your custom code here
  return $maxwidth;
}

Parameters

  • $maxwidth (int) – Maximum allowed width, default is 600.

More information

See WordPress Developer Resources: oembed_default_width

Examples

Change oEmbed default width to 800 pixels

This example sets the default maximum width of oEmbed content to 800 pixels.

add_filter('oembed_default_width', 'change_oembed_default_width');
function change_oembed_default_width($maxwidth) {
  $maxwidth = 800;
  return $maxwidth;
}

Set oEmbed default width based on screen size

This example sets the default maximum width of oEmbed content based on the user’s screen size.

add_filter('oembed_default_width', 'responsive_oembed_default_width');
function responsive_oembed_default_width($maxwidth) {
  if (wp_is_mobile()) {
    $maxwidth = 480;
  } else {
    $maxwidth = 960;
  }
  return $maxwidth;
}

Set oEmbed default width for specific post type

This example sets the default maximum width of oEmbed content to 700 pixels for posts with the post type ‘custom_post_type’.

add_filter('oembed_default_width', 'custom_post_type_oembed_default_width');
function custom_post_type_oembed_default_width($maxwidth) {
  if (get_post_type() == 'custom_post_type') {
    $maxwidth = 700;
  }
  return $maxwidth;
}

Set oEmbed default width for specific category

This example sets the default maximum width of oEmbed content to 750 pixels for posts in the category with the slug ‘large-embeds’.

add_filter('oembed_default_width', 'large_embeds_category_oembed_default_width');
function large_embeds_category_oembed_default_width($maxwidth) {
  if (has_category('large-embeds')) {
    $maxwidth = 750;
  }
  return $maxwidth;
}

Set oEmbed default width for specific user role

This example sets the default maximum width of oEmbed content to 500 pixels for authors.

add_filter('oembed_default_width', 'author_role_oembed_default_width');
function author_role_oembed_default_width($maxwidth) {
  $user = wp_get_current_user();
  if (in_array('author', $user->roles)) {
    $maxwidth = 500;
  }
  return $maxwidth;
}