skip to Main Content

Good afternoon all,

I have recently installed a new theme on my WordPress blog to get with the times as this new theme works really well on mobile whereas my current theme doesn’t.

What I like about this new theme is that uses thumbnails for the blog posts by linking to the "Featured Image" inside the blog post admin panel.

This is great, however, all of my 10,000+ articles do not have a Featured Image assigned to them as my previous theme did not use it.

I have an image that I link to at the top of my articles, which is what I use for the thumbnails when the article is shared to social media etc. These images are uploaded to a different part of my website and I link to them, so they are not stored in the WordPress Media Library.

My question now is: Is there a way that I can make it so my WordPress blog automatically uses the "first image in content" for the "Featured Image"?

I don’t think there are any plugins to do this, so I believe it is some code that I may need to change in the backend of my blog?

I look forward to a response.

Thank you.

2

Answers


  1. This plugin looks like it may suit your need well: https://wordpress.org/plugins/quick-featured-images/.

    It sets, replaces and removes featured images for hundreds of posts and pages in one go. You can run it over all contents or let it work only to desired contents by using flexible filters.

    Login or Signup to reply.
  2. The images not being in the Media Library might actually make this easier: a combination of a regular expression to identify the URL of the first image in the post, plus download_url(), wp_handle_sideload(), and wp_insert_attachment() will complete this.

    You could bypass the use of download_url() by providing the current image path to wp_handle_sideload(); that’s up to you.

    The regular expression would look something like this:

    preg_match_all('/<img.+src=['"]([^'"]+)['"].*>/i', $post->post_content, $matches);
    

    Source: https://wordpress.stackexchange.com/a/60267/59678

    Similar question’s answer with example:
    https://wordpress.stackexchange.com/a/251512/59678

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search