get_post_embed_url()WP 4.4.0

Retrieves the URL to embed a specific post in an iframe.

Hooks from the function

Return

String|false. The post embed URL on success, false if the post doesn't exist.

Usage

get_post_embed_url( $post );
$post(int|WP_Post)
Post ID or object.
Default: current post

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.4.3

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 ) );
}