Using WordPress ‘is_embed()’ PHP function

The is_embed() WordPress PHP function checks if the current query is for an embedded post.

Usage

To use the is_embed() function, simply call it in your WordPress theme or plugin:

if (is_embed()) {
    // Do something if the post is embedded
}

Parameters

  • None

More information

See WordPress Developer Resources: is_embed()

This function is included in WordPress since version 4.4.

Examples

Display a message on embedded posts

If you want to display a message on embedded posts, you can use the is_embed() function:

if (is_embed()) {
    echo '**This post is embedded!**';
}

Add a custom CSS class to embedded posts

To add a custom CSS class to the body tag of embedded posts, use the is_embed() function:

function add_embed_class($classes) {
    if (is_embed()) {
        $classes[] = 'embedded-post';
    }
    return $classes;
}
add_filter('body_class', 'add_embed_class');

Load a different template for embedded posts

To load a different template for embedded posts, use the is_embed() function:

function load_embed_template($template) {
    if (is_embed()) {
        return get_stylesheet_directory() . '/embed-template.php';
    }
    return $template;
}
add_filter('template_include', 'load_embed_template');

Remove a specific script for embedded posts

To remove a specific script for embedded posts, use the is_embed() function:

function remove_script_for_embeds() {
    if (is_embed()) {
        wp_dequeue_script('my-script-handle');
    }
}
add_action('wp_enqueue_scripts', 'remove_script_for_embeds', 100);

Disable comments for embedded posts

To disable comments for embedded posts, use the is_embed() function:

function disable_embed_comments($open, $post_id) {
    if (is_embed()) {
        return false;
    }
    return $open;
}
add_filter('comments_open', 'disable_embed_comments', 10, 2);