Function and usage of get_post_embed_html() in wordpress


Answers ( 1 )


    The get_post_embed_html() function in WordPress is used to retrieve the embed code for a specific post. This function is particularly useful when you want to embed a WordPress post into another post, page, or even an external website. Here's a detailed explanation of the function and its usage:

    Function Signature

    get_post_embed_html( int $width, int $height, int|WP_Post $post = null ): string|false


    1. $width (int, required): This parameter sets the width of the embed. It's a required parameter, and you need to specify the width in pixels.

    2. $height (int, required): Similar to $width, this parameter sets the height of the embed in pixels and is also required.

    3. $post (int | WP_Post, optional): This can be either the post ID or a WP_Post object. If not provided, the global $post object is used by default. This allows the function to automatically use the current post in the WordPress loop.

    Return Value

    • string|false: The function returns the embed code as a string if successful. If the post does not exist or the embed code cannot be generated for some reason, it returns false.

    Example Usage

    Basic Usage

    Here's a basic example of how you might use get_post_embed_html():

    // Define width and height for the embed
    $width = 600;
    $height = 400;
    // Use the current post
    $embed_code = get_post_embed_html($width, $height);
    if ( $embed_code !== false ) {
        echo $embed_code; // Output the embed code
    } else {
        echo 'Unable to generate embed code for this post.';

    Specifying a Post

    You can also specify a particular post by its ID:

    $width = 600;
    $height = 400;
    $post_id = 123; // Replace with the actual post ID
    $embed_code = get_post_embed_html($width, $height, $post_id);
    if ( $embed_code !== false ) {
        echo $embed_code;
    } else {
        echo 'Post does not exist or embed code cannot be generated.';


    • This function is part of the WordPress Embeds feature.
    • Make sure to validate and sanitize inputs if you're accepting them from users to avoid security issues.
    • The function might not work as expected for custom post types unless they support embeds.


    get_post_embed_html() is a handy function for embedding WordPress posts within other content. By specifying the dimensions and optionally the post to embed, you can easily integrate dynamic WordPress content into various parts of your site or even external sites.

Leave an answer