Using WordPress ’embed_thumbnail_image_shape’ PHP filter

The embed_thumbnail_image_shape WordPress PHP filter allows you to modify the shape of the thumbnail image in the embed template.

Usage

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

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

Parameters

  • $shape (string) – The thumbnail image shape, either ‘rectangular’ or ‘square’.
  • $thumbnail_id (int) – The attachment ID for the thumbnail image.

More information

See WordPress Developer Resources: embed_thumbnail_image_shape

Examples

Change Thumbnail Shape to Rectangular

Make all thumbnail images rectangular.

add_filter('embed_thumbnail_image_shape', 'change_thumbnail_to_rectangular', 10, 2);

function change_thumbnail_to_rectangular($shape, $thumbnail_id) {
    $shape = 'rectangular';
    return $shape;
}

Change Thumbnail Shape to Square

Make all thumbnail images square.

add_filter('embed_thumbnail_image_shape', 'change_thumbnail_to_square', 10, 2);

function change_thumbnail_to_square($shape, $thumbnail_id) {
    $shape = 'square';
    return $shape;
}

Set Thumbnail Shape Based on Post Category

Change thumbnail shape depending on the post’s category.

add_filter('embed_thumbnail_image_shape', 'set_thumbnail_shape_based_on_category', 10, 2);

function set_thumbnail_shape_based_on_category($shape, $thumbnail_id) {
    $post_id = get_post_thumbnail_id($thumbnail_id);
    if (has_category('rectangle', $post_id)) {
        $shape = 'rectangular';
    } elseif (has_category('square', $post_id)) {
        $shape = 'square';
    }
    return $shape;
}

Set Thumbnail Shape Based on Image Dimensions

Change thumbnail shape based on the image’s dimensions.

add_filter('embed_thumbnail_image_shape', 'set_thumbnail_shape_based_on_dimensions', 10, 2);

function set_thumbnail_shape_based_on_dimensions($shape, $thumbnail_id) {
    $image_data = wp_get_attachment_metadata($thumbnail_id);
    if ($image_data['width'] > $image_data['height']) {
        $shape = 'rectangular';
    } else {
        $shape = 'square';
    }
    return $shape;
}

Set Thumbnail Shape for Custom Post Type

Change thumbnail shape for a specific custom post type.

add_filter('embed_thumbnail_image_shape', 'set_thumbnail_shape_for_custom_post_type', 10, 2);

function set_thumbnail_shape_for_custom_post_type($shape, $thumbnail_id) {
    $post_id = get_post_thumbnail_id($thumbnail_id);
    if ('custom_post_type' == get_post_type($post_id)) {
        $shape = 'square';
    }
    return $shape;
}