Using WordPress ’embed_template’ PHP filter

The embed_template WordPress PHP filter allows you to modify the template used for embedded posts.

Usage

add_filter('embed_template', 'your_custom_function_name');
function your_custom_function_name($template) {
    // your custom code here
    return $template;
}

Parameters

  • $template (string) – Path to the template file.

More information

See WordPress Developer Resources: embed_template

Examples

Change the template for embedded posts

Modify the default template for embedded posts by using a custom template file.

add_filter('embed_template', 'change_embed_template');
function change_embed_template($template) {
    $custom_template = locate_template('custom-embed.php');
    if ($custom_template) {
        return $custom_template;
    }
    return $template;
}

Set a custom template for a specific post type

Use a different template for embedded posts of a specific custom post type.

add_filter('embed_template', 'custom_post_type_embed_template');
function custom_post_type_embed_template($template) {
    global $post;
    if ('custom_post_type' === $post->post_type) {
        $custom_template = locate_template('custom-post-type-embed.php');
        if ($custom_template) {
            return $custom_template;
        }
    }
    return $template;
}

Change the template based on the post category

Set a custom template for embedded posts from a specific category.

add_filter('embed_template', 'category_based_embed_template');
function category_based_embed_template($template) {
    if (has_category('special', get_the_ID())) {
        $custom_template = locate_template('special-category-embed.php');
        if ($custom_template) {
            return $custom_template;
        }
    }
    return $template;
}

Use a custom template for logged-in users

Change the embedded post template for logged-in users.

add_filter('embed_template', 'logged_in_user_embed_template');
function logged_in_user_embed_template($template) {
    if (is_user_logged_in()) {
        $custom_template = locate_template('logged-in-embed.php');
        if ($custom_template) {
            return $custom_template;
        }
    }
    return $template;
}

Modify the template for a specific post

Set a custom template for a specific post ID.

add_filter('embed_template', 'specific_post_embed_template');
function specific_post_embed_template($template) {
    if (get_the_ID() == 123) {
        $custom_template = locate_template('post-123-embed.php');
        if ($custom_template) {
            return $custom_template;
        }
    }
    return $template;
}