Skip to Content
Menu

get_thumbnail_src()

Get only the URL for the specific thumbnail sizes of an ID, or from the attachment ID directly.

PHP November 17, 2020

Usage

PHP
nebula()->get_thumbnail_src($id, $size, $type)

Parameters

$id
(Required) (Integer) The post ID
Default: None

$size
(Optional) (String) The size name to get
Default: full

$type
(Optional) (String) Whether the ID is a post or attachment
Default: post

Parameter Notes

The $size parameter can contain any default WordPress or custom image size name.

Default WordPress Image Sizes:

  • “thumbnail” (150px square)
  • “medium” (max 300px width)
  • “large” (max 1024px width)
  • “full” (original image)

Be careful using “full” (which is the default for this function) because if an enormous image is uploaded, that is the file that will be loaded on the frontend!

Default Nebula Image Sizes:

  • “square” (512px square)
  • “open_graph_large” (1200 x 630px)
  • “open_graph_small” (600 x 315px)
  • “twitter_large” (280 x 150px)
  • “twitter_small” (200px square)

Remember: if the source image is not large enough, that image size will not be created.

Request or provide clarification »

Examples

PHP
<?php echo nebula()->get_thumbnail_src(get_the_post_thumbnail($post->ID, 'twitter_large')); ?>
PHP
<?php echo nebula()->get_thumbnail_src($post->ID, 'twitter_large'); ?>

Using as an inline background image

HTML
<div style="background: url(<?php echo nebula()->get_thumbnail_src(get_the_id()); ?>) no-repeat bottom right;"></div>

Get a specific size of an attachment image (like from an ACF image field)

PHP
<?php echo nebula()->get_thumbnail_src(get_field('thumbnail_image'), 'twitter_large', 'attachment'); ?>
Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Functions.php on line 404.

    1 Hook

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "nebula_thumbnail_src_size"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    Note: This function contains 1 to-do comment.

    PHP
            public function get_thumbnail_src($img=null, $size='full', $type='post'){
                if ( empty($img) ){
                    return false;
                }
    
                //If HTML is passed, immediately parse it with HTML
                if ( strpos($img, '<img') !== false ){ //@todo "Nebula" 0: Update strpos() to str_contains() in PHP8
                    return ( preg_match('~\bsrc="([^"]++)"~', $img, $matches) )? $matches[1] : ''; //Pull the img src from the HTML tag itself
                }
    
                $id = intval($img); //Can now use the ID
    
                //If and ID was not passed, immediately return it (in case it is already an image URL)
                if ( $id === 0 || ($id === 1 && $img != 1) ){
                    return $img;
                }
    
                $size = apply_filters('nebula_thumbnail_src_size', $size, $id);
    
                //If an attachment ID (or thumbnail ID) was passed
                if ( get_post_type($id) === 'attachment' || $type !== 'post' ){
                    $image = wp_get_attachment_image_src(get_post_thumbnail_id($id), $size);
    
                    if ( !empty($image[0]) ){
                        return $image[0];
                    }
                }
    
                //Otherwise get the HTML from the post ID (or if the attachment src did not work above)
                $img_tag = get_the_post_thumbnail($id, $size);
                if ( get_post_type($id) === 'attachment' ){
                    $img_tag = wp_get_attachment_image($id, $size);
                }
    
                return ( preg_match('~\bsrc="([^"]++)"~', $img_tag, $matches) )? $matches[1] : ''; //Pull the img src from the HTML tag itself
            }
    

    Override

    This function can not be short-circuited with an override filter. Request one?