get_post_embed_url()WP 4.4.0

Gets the URL to be used in an iframe for embedding the specified post on another site (oEmbed format).

What is Embed read here: oEmbed in WordPress

To embed a post from another site on WordPress into your site, you need to add the post's URL to the content of your post. WordPress will automatically process the URL.

Hooks from the function

Returns

String|false. oEmbed URL of the post or false if the post does not exist.

Usage

get_post_embed_url( $post );
$post(integer/WP_Post)
ID or post object, the oEmbed url of which needs to be obtained.
Default: null (the current post in the loop)

Examples

0

#1 What oEmbed link looks like

echo get_post_embed_url( 1 );
// outputs: http://example.com/post-name/embed
0

#2 Finished embedded view

As an example, let's take the article on this site: https://wp-kama.com/handbook/cheatsheet and create a manual embed for it, using this URL https://wp-kama.com/handbook/cheatsheet/embed

<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"
src="/handbook/cheatsheet/embed"
width="600" height="350" style="max-width:600px"
frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>

The result will be this insertion:

The following function exists to create an iframe block automatically for the specified post: get_post_embed_html()

Changelog

Since 4.4.0 Introduced.

get_post_embed_url() code WP 6.9

function get_post_embed_url( $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$embed_url     = trailingslashit( get_permalink( $post ) ) . user_trailingslashit( 'embed' );
	$path_conflict = get_page_by_path( str_replace( home_url(), '', $embed_url ), OBJECT, get_post_types( array( 'public' => true ) ) );

	if ( ! get_option( 'permalink_structure' ) || $path_conflict ) {
		$embed_url = add_query_arg( array( 'embed' => 'true' ), get_permalink( $post ) );
	}

	/**
	 * Filters the URL to embed a specific post.
	 *
	 * @since 4.4.0
	 *
	 * @param string  $embed_url The post embed URL.
	 * @param WP_Post $post      The corresponding post object.
	 */
	return sanitize_url( apply_filters( 'post_embed_url', $embed_url, $post ) );
}