Using WordPress ’embed_thumbnail_image_size’ PHP filter

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

Usage

add_filter('embed_thumbnail_image_size', 'your_custom_function', 10, 2);

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

Parameters

  • $image_size (string): Thumbnail image size.
  • $thumbnail_id (int): Attachment ID.

More information

See WordPress Developer Resources: embed_thumbnail_image_size

Examples

Change Embed Thumbnail Size

Modify the thumbnail size for the embedded posts.

add_filter('embed_thumbnail_image_size', 'change_embed_thumbnail_size', 10, 2);

function change_embed_thumbnail_size($image_size, $thumbnail_id) {
    $image_size = 'medium';
    return $image_size;
}

Set Custom Image Size for Specific Attachment ID

Change the thumbnail size for a specific attachment ID in the embedded posts.

add_filter('embed_thumbnail_image_size', 'set_custom_image_size_for_attachment', 10, 2);

function set_custom_image_size_for_attachment($image_size, $thumbnail_id) {
    if ($thumbnail_id == 42) {
        $image_size = 'large';
    }
    return $image_size;
}

Set Image Size Based on Post Type

Change the thumbnail size based on the post type in the embedded posts.

add_filter('embed_thumbnail_image_size', 'set_image_size_based_on_post_type', 10, 2);

function set_image_size_based_on_post_type($image_size, $thumbnail_id) {
    $post_type = get_post_type($thumbnail_id);
    if ($post_type == 'event') {
        $image_size = 'large';
    }
    return $image_size;
}

Set Different Image Sizes for Portrait and Landscape Images

Adjust the thumbnail size depending on whether the image is in portrait or landscape orientation.

add_filter('embed_thumbnail_image_size', 'set_size_based_on_image_orientation', 10, 2);

function set_size_based_on_image_orientation($image_size, $thumbnail_id) {
    $image_data = wp_get_attachment_metadata($thumbnail_id);
    if ($image_data['width'] > $image_data['height']) {
        $image_size = 'landscape-size';
    } else {
        $image_size = 'portrait-size';
    }
    return $image_size;
}

Force Square Image Size

Force a square image size for the embedded post thumbnails.

add_filter('embed_thumbnail_image_size', 'force_square_image_size', 10, 2);

function force_square_image_size($image_size, $thumbnail_id) {
    $image_size = 'square-size';
    return $image_size;
}