I am developing a WordPress theme for WordPress.org. Do I need to escape get_the_post_thumbnail function? I want to use it outside the loop. So, I won't be able to use the_post_thumbnail. Please let me know if I need to escape get_the_post_thumbnail and if yes, how do I do it?

Thanks in advance.


No, you don't have to escape the data. And it doesn't need to be escaped. Please take a look at the_post_thumbnail() function, it didn't do any escaping. So no worries.


In WordPress, the functions start with get_ generally return data while non-prefixed counterparts echo (i.e. print) data.

the_post_thumbnail() works only in the post code (loop) and return featured an image of the current post.

get_the_post_thumbnail() works everywhere and get $post_id parameter at first attribute.

For eaxample:

the_post_thumbnail($params) = get_the_post_thumbnail($current_post_id,$params);

This is realization in WP engine:

function the_post_thumbnail ($size='post-thumbnail', $attr=''){
    echo get_the_post_thumbnail( null, $size, $attr );

function get_post_thumbnail_id( $post_id = null ) {
    $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
    return get_post_meta( $post_id, '_thumbnail_id', true );

if $post_id is NULL - used get_the_ID() of current post.

I think you don't need to escape get_the_post_thumbnail function.

  • Hey @nilesh, please this Jun 8 '18 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.