The get_post_embed_url() WordPress PHP function retrieves the URL to embed a specific post in an iframe.
Usage
get_post_embed_url( $post );
Example:
Input:
echo get_post_embed_url( 42 );
Output:
https://example.com/?p=42&embed=true
Parameters
$post (int|WP_Post)
: Optional. Post ID or object. Defaults to the current post. Default value:null
.
More information
See WordPress Developer Resources: get_post_embed_url()
Examples
Embedding the current post
Retrieve the embed URL for the current post and display it in an iframe.
// Get the embed URL for the current post $embed_url = get_post_embed_url(); // Display the embed URL in an iframe echo '<iframe src="' . esc_url( $embed_url ) . '" width="600" height="400" frameborder="0"></iframe>';
Embedding a specific post by ID
Retrieve the embed URL for a specific post using its ID and display it in an iframe.
// Post ID $post_id = 42;// Get the embed URL for the specific post $embed_url = get_post_embed_url( $post_id );
// Display the embed URL in an iframe echo '<iframe src="' . esc_url( $embed_url ) . '" width="600" height="400" frameborder="0"></iframe>';
Embedding a post using a WP_Post object
Retrieve the embed URL for a specific post using a WP_Post
object and display it in an iframe.
// Get the WP_Post object for a specific post ID $post_object = get_post( 42 ); // Get the embed URL using the WP_Post object $embed_url = get_post_embed_url( $post_object ); // Display the embed URL in an iframe echo '<iframe src="' . esc_url( $embed_url ) . '" width="600" height="400" frameborder="0"></iframe>';
Displaying multiple post embed URLs
Loop through an array of post IDs and display their embed URLs in iframes.
// Array of post IDs $post_ids = array( 42, 56, 89 ); // Loop through the post IDs foreach ( $post_ids as $post_id ) { // Get the embed URL for each post $embed_url = get_post_embed_url( $post_id ); // Display the embed URL in an iframe echo '<iframe src="' . esc_url( $embed_url ) . '" width="600" height="400" frameborder="0"></iframe>'; }
Embedding a post in a template file
In a template file (e.g., single.php
), retrieve the embed URL for the current post and display it in an iframe.
// Get the embed URL for the current post $embed_url = get_post_embed_url(); // Display the embed URL in an iframe echo '<iframe src="' . esc_url( $embed_url ) . '" width="100%" height="400" frameborder="0"></iframe>';