Using WordPress ‘oembed_fetch_url’ PHP filter

The oembed_fetch_url WordPress PHP filter allows you to modify the oEmbed URL that is fetched for embedding content.

Usage

add_filter('oembed_fetch_url', 'my_custom_oembed_fetch_url', 10, 3);
function my_custom_oembed_fetch_url($provider, $url, $args) {
  // your custom code here
  return $provider;
}

Parameters

  • $provider (string) – URL of the oEmbed provider.
  • $url (string) – URL of the content to be embedded.
  • $args (array) – Additional arguments for retrieving embed HTML. See wp_oembed_get() for accepted arguments. Default empty.

More information

See WordPress Developer Resources: oembed_fetch_url

Examples

Change oEmbed provider

Change the oEmbed provider for a specific content URL.

add_filter('oembed_fetch_url', 'change_oembed_provider', 10, 3);
function change_oembed_provider($provider, $url, $args) {
  if ($url == 'https://example.com/specific-content/') {
    return 'https://custom-oembed-provider.com/oembed';
  }
  return $provider;
}

Add query parameters to provider URL

Add custom query parameters to the oEmbed provider URL.

add_filter('oembed_fetch_url', 'add_query_params_to_provider', 10, 3);
function add_query_params_to_provider($provider, $url, $args) {
  $provider = add_query_arg('custom_param', 'value', $provider);
  return $provider;
}

Change oEmbed provider based on content type

Use a different oEmbed provider for video content.

add_filter('oembed_fetch_url', 'change_oembed_provider_for_videos', 10, 3);
function change_oembed_provider_for_videos($provider, $url, $args) {
  if (strpos($url, 'video') !== false) {
    return 'https://video-oembed-provider.com/oembed';
  }
  return $provider;
}

Disable oEmbed discovery

Disable oEmbed discovery when the provider URL is not found in the built-in providers list.

add_filter('oembed_fetch_url', 'disable_oembed_discovery', 10, 3);
function disable_oembed_discovery($provider, $url, $args) {
  $args['discover'] = false;
  return $provider;
}

Set maximum width for embeds

Set a custom maximum width for all embeds.

add_filter('oembed_fetch_url', 'set_custom_max_width', 10, 3);
function set_custom_max_width($provider, $url, $args) {
  $args['width'] = 800;
  return $provider;
}