Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Avoid No Thumbnail Issue While Sharing Post on Facebook

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Avoid No Thumbnail Issue While Sharing Post on Facebook

Have you ever tried clicking on a Facebook Share link from a blog post just to end up with either no thumbnail, or very unrelated thumbnail showing up? This is exactly what happened to one of our client’s site. For them, we simply wrote a manual code and placed into their header.php, but while surfing through twitter we came across a better solution. In this article, we will show you how to avoid no thumbnail or unrelated thumbnail issue while sharing posts on facebook.

One of the reason why you get no thumbnail, or unrelated thumbnail is because facebook’s script is not smart enough. On each page load, it searches for all image tags and display the ones that it finds useful. Sometimes the script times out without even loading the right image. To get around this issue, facebook recommends developers to tell the script exactly which image to pick by adding a meta tag in the header. The meta tag looks something like this:

<meta property="og:image" content=""/>

This works perfectly when you are running a static site, but for a dynamic platform like WordPress, we need to tweak the code a little bit to make it work. We are going to use the WordPress Post Thumbnail (aka featured image) and tell facebook script to pick that as the main image. If for some reason you did not attach a featured image to a post, then we will tell facebook to pick a default image that we have pre-selected.

You need to open your theme’s functions.php file and paste the following code within the php tags:

function insert_image_src_rel_in_head() {
	global $post;
	if ( !is_singular()) //if it is not a post or a page
	if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
		$default_image=""; //replace this with a default image on your server or an image in your media library
		echo '<meta property="og:image" content="' . $default_image . '"/>';
		$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
		echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
	echo "
add_action( 'wp_head', 'insert_image_src_rel_in_head', 5 );

This code will add a meta tag only on single post pages with the thumbnail url in the meta tag. We have specified a default image “”, so make sure you change that URL in the code for your image.

For those users who do not want to mess with the codes, there is a plugin for you to use called Add image_src Meta Tag

This function will solve your no thumbnail issue with facebook sharing.

Source: Staenz Web Solutions

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. sadiq surathiya says:

    Really Helpfull !!! :) Thanks fixed my amp thumbnail with this issue.

  2. Jamie says:

    I did this on my site and now my site isn’t working…. HELP!!!!

  3. Peter says:

    You guys ROCK!

  4. Tushar Tripathi says:

    I was having the same problem of no display of preview image, so i came to ours post and copied this post on my function.php site. After clicking updating. Everything is gone. Now my site is not working………..

  5. Thomas says:

    Thanks a lot! 4 years later but it works perfectly!

  6. Michelle Sweeten says:

    thank you so much! I’m pretty scared of inserting things into my code, but this worked very well! The plug in is gone, so I tried inserting and it was very easy.

  7. Yohan Perera says:

    I had this problem in my own blog as well as 2 other blogs which I support. I could not find the plugin you have mentioned so I tried “Facebook Open Graph, Google+ and Twitter Card Tags” plugin which solved the problem. Thank you for the clue.

  8. Crinan says:

    THANK YOU!!!!!
    It works, when all the plugins failed.

  9. Bhagirath Kumar says:

    Hi even I put the code which you suggested in “function.php” file but still I am facing same issue.
    for my wordpress we have to id to login in one ID its working fine but for other ID it’s creating the problem. Please help in in this issue.

    Please find the link in which I am facing the issue.

  10. Swattt says:

    Hi Can we add this code via Insert Header and Footer Plugin

    • WPBeginner Support says:

      You can add the meta tag using Insert Header and Footer plugin. The second code snippet should go in functions.php or a site-specific plugin.

      • Swattt says:

        Warning: Cannot modify header information – headers already sent by (output started at /home/picko9mr/public_html/ in /home/picko9mr/public_html/ on line 1196

        hi i am getting above error.. and even after removing that error still cant see thumbnail & Description

        • Swattt says:

          and even after removing/deleting that from Function theme its still showing in (inc/function.php)

  11. Missie says:

    I cannot thank you enough for this! I have been trying to fix my problem for hours now (well, more like days but i kept giving up) and this solved it! Thank you thank you thank you!!

  12. Ashley says:

    I am having the same problem but with tumblr not facebook. it doesn’t show the url image any time i link from my website, is this a settings issue or is there a work-a-round?

  13. Maria says:

    This code is really great! I have some issues with meta description. When I share my article on Facebook, image preview and title appear without meta description. How can I solve it? Thanks!

    • harsha says:

      Hi Maria,

      Which theme are you using also assure you have your meta description for your posts before you publish it!

  14. Cathy Brabant says:

    I would like to know how to delete the picture that comes with an article. I just want the article and not the picture to be posted on FB.

    • WPBeginner Support says:

      When sharing an article on Facebook you have the option to remove the image. However, if you are using automatic sharing tools then Facebook will automatically try to fetch a suitable image.

  15. SK Raja says:

    Superb. It worked in the first attempt. Thanks for the sharing the tips.

  16. Thomas Frick says:

    My problem is i do not want a thumbnail to open in my comment at all, i want a full size pic. The pics in the thumbnails are very unclear, and text is totally obliterated. Why would they even make it like this. Is there any reason anyone in the world would want their pic to be reduced to a barely viewable thumbnail???

  17. Zaheer Abbas Aghani says:

    Above code not working for me ??

  18. Dawn says:

    Thank you so much for this code! It’s exactly the solution I’m looking for, only I haven’t been able to get it to work for me. I used the code from this post and followed the Site-Specific tutorial on this site. I successfully created and activated the plugin from my WP dashboard, but I have done something wrong because it doesn’t work.

    I am using JetPack for social sharing buttons, but even when I manually copy the code into Facebook, my custom thumbnail does not show up. (I thought maybe the plugins weren’t working well together.)

    I am not clear on how to correctly wrap code in php tags, so maybe I pasted something incorrectly? Thank you for your attention to this matter and the time you’ve taken for writing tutorials.

    • WPBeginner Support says:

      Looking at your site, seems like your Facebook open graph meta tag for post image appears correctly. However, it is showing a blank image. This is probably because you are not using an image as featured image and one of your plugins handling open graph functionality is adding a blank image. Try setting up an image as featured image and see what happens.

      • Dawn says:

        Thank you for your reply. :) It works now, although I can’t quite figure out why. I did clean up my plug-ins, but am unsure at what point exactly things started working. Anyway, I love it. Thank you.

  19. Zsofc says:

    thank you!!! The plugin solved my problem!!

  20. Zaheer Abbas says:

    Code just not working for me same result

  21. Patrik says:

    Plese help,

    I did as instructed and now I have a white screen of death, even on the admin page so I am not able to log in to remove that part from the functions.php and idea what can I do in this case ? :(

  22. Kaley Perkins says:

    The plugin hasn’t been updated in over four years now. Sadness and grief :-(

  23. WPBeginner Staff says:

    Access your website using File Manager in cpanel or an FTP client. Locate the file where you added the code. Edit that file and remove the code.

  24. Katti Lingmar says:

    tried to paste this on my wp-blog an now all I get is

    “Parse error: syntax error, unexpected T_FUNCTION in
    on line 7” can’t acsess my blog at all.
    crying here…

  25. GRTR666 says:

    Hi, i put that code in my template but when i want to share a link through Facebook, I need to refresh the page 3 times and then it loads the correct image (thumbnail of the post), do you know this issue ??

    BTW even adding : $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), ‘full’ );

    The thumbnail size is 120x90px

    Do you know why ?

    Thank you

  26. Morgan says:

    I tried doing this and got the white screen of death. Found the theme function file in the Bluehost file manager and deleted it, thus bringing my site back to life (RELIEF!!!!!).

    Was I supposed to add the code in-between the “//” which was between the .php ending code? I put it after the “//”. I also wonder if the link to the default picture that I chose was faulty.

  27. Ruth Cataldo says:

    How do I find the php tags? I have located theme’s functions.php file but where are the tags?? I don’t want to paste it in the wrong part!

  28. Brendan Lee says:

    Hey guys, great easy to understand article, thanks.

    Unluckily this did not work for me. A little background: I first noticed the problem on this post of mine:

    That is my first and only post since migrating my site to a new host, and is still the only post that is having this problem.

    All my posts that were published before the migration still generate thumbnails fine.

    As a test, I went back and uploaded a new featured image for one of my old posts:

    As soon as I did that, it stopped working for this post as well. Even after I changed the featured image back to the original, it still doesn’t work.

    Any ideas? Been losing my mind over this!

    Thanks so much

  29. Misa Mladenovic says:

    Just doesnt work for me. FB picks up random banner and crops it ugly :(

  30. Abidemi says:

    Hey i tried the code but my entire page turned white and now I can’t log in. Any solution?? Plus other solutions other than using the code? Thank you

  31. Ali says:

    I’ve been having this problem for months now and had no idea how or why, I’m so glad I searched your site for the answer!
    I am going to try this tonight…(thank you!!) I only hope I can make it work (I will do the plugin because I dont know where to find functions.php?)

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.