Using WordPress ‘post_embed_url’ PHP filter

The post_embed_url WordPress PHP filter modifies the URL used to embed a specific post.

Usage

add_filter('post_embed_url', 'your_custom_function', 10, 2);

function your_custom_function($embed_url, $post) {
  // your custom code here
  return $embed_url;
}

Parameters

  • $embed_url: string – The original post embed URL.
  • $post: WP_Post – The corresponding post object.

More information

See WordPress Developer Resources: post_embed_url

Examples

Change embed URL domain

Change the domain for the embed URL to a custom domain.

add_filter('post_embed_url', 'change_embed_url_domain', 10, 2);

function change_embed_url_domain($embed_url, $post) {
  $parsed_url = parse_url($embed_url);
  $custom_domain = 'https://embed.example.com';
  $new_embed_url = $custom_domain . $parsed_url['path'];
  return $new_embed_url;
}

Add custom query parameter to embed URL

Add a custom query parameter to the embed URL.

add_filter('post_embed_url', 'add_custom_query_param', 10, 2);

function add_custom_query_param($embed_url, $post) {
  $new_embed_url = add_query_arg('custom_param', 'value', $embed_url);
  return $new_embed_url;
}

Remove specific query parameters from embed URL

Remove specific query parameters from the embed URL.

add_filter('post_embed_url', 'remove_query_params', 10, 2);

function remove_query_params($embed_url, $post) {
  $new_embed_url = remove_query_arg(array('param1', 'param2'), $embed_url);
  return $new_embed_url;
}

Use post slug in embed URL instead of ID

Replace the post ID in the embed URL with the post slug.

add_filter('post_embed_url', 'use_post_slug_in_embed_url', 10, 2);

function use_post_slug_in_embed_url($embed_url, $post) {
  $new_embed_url = str_replace($post->ID, $post->post_name, $embed_url);
  return $new_embed_url;
}

Disable embed URL for specific post types

Disable the embed URL for a specific post type, returning an empty string.

add_filter('post_embed_url', 'disable_embed_url_for_post_type', 10, 2);

function disable_embed_url_for_post_type($embed_url, $post) {
  if ($post->post_type === 'custom_post_type') {
    return '';
  }
  return $embed_url;
}