How to Set the Thumbnail Source in WordPress: A Step-by-Step Guide

Home » Snippets » How to Set the Thumbnail Source in WordPress: A Step-by-Step Guide

Created with:



Creator: WPTurbo Team

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project

Have you ever wondered where the thumbnails on your WordPress site originate from? Or perhaps you’re looking to alter your site’s default thumbnail source for a customized, cohesive look. No matter your reasons, understanding and manipulating the thumbnail source in WordPress can dramatically enhance your website aesthetics and user experience. This article will provide you with a step-by-step guide to understanding and altering the thumbnail source on your WordPress site.

					function wpturbo_get_thumbnail_src() {
    $thumbID = get_post_thumbnail_id( $post->ID );
    $imgDest = wp_get_attachment_image_src( $thumbID, 'full' );
    if ($imgDest) {
       echo $imgDest[0];
add_action( 'after_theme_setup' , 'wpturbo_get_thumbnail_src' );

The function wpturbo_get_thumbnail_src() is first declared. The purpose of this function is to fetch and display the URL source of the thumbnail of a post in WordPress. This might be needed for any number of reasons – perhaps to dynamically alter the thumbnail or use it elsewhere outside of the general post thumbnail context.

Inside the function, we first have $thumbID = get_post_thumbnail_id( $post->ID );. This line of the code is making use of the WordPress built-in function get_post_thumbnail_id(), with $post->ID as a parameter. The function is used to retrieve the featured image ID of the current post.

Next, $imgDest = wp_get_attachment_image_src( $thumbID, 'full' );. In this line, we’re using another WordPress-built function wp_get_attachment_image_src(). This function is provided with two parameters: the $thumbID, which we declared earlier to hold the value of the post’s thumbnail ID, and 'full', which is instructing WordPress that we want the full size of the image. This function wp_get_attachment_image_src() returns an array of the URL of the attachment image source, its width and height.

In the following conditional statement if ($imgDest), we’re checking whether $imgDest exists or has valid content to prevent errors for posts that might not have a set thumbnail.

Inside the conditional statement, echo $imgDest[0]; is responsible for outputting or displaying the URL of the image. We’re accessing the first element [0] of the array $imgDest, as this element holds the URL of the image source. We make use of echo to output or display that URL.

Lastly, the wpturbo_get_thumbnail_src() function is hooked into the after_theme_setup action. It’s done this way with add_action( 'after_theme_setup' , 'wpturbo_get_thumbnail_src' );. This helps to ensure that the function is only activated after the theme setup process, achieving an optimal load sequence. This would make sure our function is available and running when themes are setting up their post types and taxonomies etc.

Register an account to save your snippets or go Pro to get more features.