Using WordPress ’embed_thumbnail_id’ PHP filter

The embed_thumbnail_id WordPress PHP filter allows you to modify the thumbnail image ID used in the embed template.

Usage

add_filter('embed_thumbnail_id', 'your_custom_function', 10, 1);

function your_custom_function($thumbnail_id) {
    // your custom code here
    return $thumbnail_id;
}

Parameters

  • $thumbnail_id (int|false): The attachment ID of the thumbnail image or false if there is none.

More information

See WordPress Developer Resources: embed_thumbnail_id

Examples

Change the default thumbnail for embeds

This example changes the default thumbnail for embeds to a specific attachment ID.

add_filter('embed_thumbnail_id', 'set_default_embed_thumbnail', 10, 1);

function set_default_embed_thumbnail($thumbnail_id) {
    // Set the default attachment ID
    $default_thumbnail_id = 42;
    if (false === $thumbnail_id) {
        return $default_thumbnail_id;
    }
    return $thumbnail_id;
}

Remove the thumbnail from embeds

This example removes the thumbnail image from all embeds.

add_filter('embed_thumbnail_id', 'remove_thumbnail_from_embeds', 10, 1);

function remove_thumbnail_from_embeds($thumbnail_id) {
    return false;
}

Use a custom thumbnail for specific post types

This example sets a custom thumbnail for a specific post type in embeds.

add_filter('embed_thumbnail_id', 'custom_thumbnail_for_specific_post_type', 10, 1);

function custom_thumbnail_for_specific_post_type($thumbnail_id) {
    global $post;
    if ('custom_post_type' === $post->post_type) {
        $custom_thumbnail_id = 99;
        return $custom_thumbnail_id;
    }
    return $thumbnail_id;
}

This example sets the featured image of the parent page as the thumbnail for child pages in embeds.

add_filter('embed_thumbnail_id', 'parent_featured_image_for_child_page', 10, 1);

function parent_featured_image_for_child_page($thumbnail_id) {
    global $post;
    if ($post->post_parent) {
        $parent_thumbnail_id = get_post_thumbnail_id($post->post_parent);
        if ($parent_thumbnail_id) {
            return $parent_thumbnail_id;
        }
    }
    return $thumbnail_id;
}

Randomize the thumbnail for embeds

This example randomizes the thumbnail image for embeds from a list of attachment IDs.

add_filter('embed_thumbnail_id', 'randomize_embed_thumbnail', 10, 1);

function randomize_embed_thumbnail($thumbnail_id) {
    $random_thumbnail_ids = array(10, 20, 30, 40, 50);
    $random_key = array_rand($random_thumbnail_ids);
    return $random_thumbnail_ids[$random_key];
}