• Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

Easy Web Design Tutorials

WordPress Tutorials and more

  • Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

30 June - 2016 By Paal Joachim 2 Comments
Last updated on: July 12, 2016

Automatically setting the featured image without a plugin

Set first post image as featured when there is no featured image.

Set first post image as featured when there is no featured image.

 

It can be very easy to forget to set the featured image. Here is code that will do the following:
1. Sets the featured image.
2. If no featured image then look for category image.
3. If no category image set the first post image.
3. If no post image it will set a fallback image.

I did a lot of searching online and came across an article from
https://wpforce.com/automatically-set-the-featured-image-in-wordpress/ and https://wpsites.net/web-design/add-default-featured-image-for-each-post-in-a-category/

Wpforce had most of the code. I took the category code from wpsites. I then merged the category code into the featured images code.

The attachment id
Since the code uses post-id/attachment id one needs to find the image data id. Go to the media library and right click the image you want to find the data id to. Select Inspect Element then look into the html code and you should notice the data-id number of the image. This is the number you need to use.

Add the following code into your functions file.

 

If you do not want to add the else if categories then adjust the first else if to:

} else {
 set_post_thumbnail($post->ID, '21'); // Find attachment media id by right clicking the image in the Media library and selecting inspect element. Look for the data-id number. This number is then added to the post id.
 }

Then remove each else if that follows.

 

I also have a code specific for Genesis themes.  (It seems to work better then the general featured image code above. I am looking into taking the Genesis featured images code and making it work for most themes in addition to Genesis themes.) This code looks for images through the url.

 

Removing the featured images

If you need to remove your featured images (I had to when I was testing the above featured images code to clear the cache in WordPress.)

add_action ('init', 'remove_featured_images' );
function remove_featured_images () {
global $wpdb;
// The following query will only unset the featured image, it will not delete it. 
$wpdb->query( "
 DELETE FROM $wpdb->postmeta 
 WHERE meta_key = '_thumbnail_id'
" );
}

The code is from: https://spicemailer.com/wordpress/how-to-remove-featured-image-from-all-posts-wordpress/ There is also variations of the above code located at the spicemailer web site.

 

Plugins

Default featured image plugin
Categories images plugin
Easy featured images plugin
wpmudev tutorial for easy featured images plugin

 

Additional resources to learn from:

.https://www.sitepoint.com/how-to-add-featured-image-thumbnails-to-your-wordpress-theme/

wordpress.stackexchange
https://pastebin.com/34WFEMXd
https://github.com/devinsays/auto-set-featured-image/blob/master/auto-set-featured-image.php
https://paulund.co.uk/get-wordpress-image-id-by-url

 

I am working on figuring out how to set the URL/folder location of a fallback/default image instead of having to find the attachment image id. I will post the code for that when I have it.

Share this:

  • Email

Categories: Developer, WordPress

Paal Joachim Romdahl

I enjoy teaching and creating tutorials. As well creating web sites.
I help people gain WordPress knowledge through my easy to follow tutorials and specialized training. Contact me for more information on how I can improve your WordPress skills and to help get your web site quickly up and running.

Comments

  1. Simeon says

    10 April - 2017

    Hey Paal,

    Is this still valid for the newest WP version? I tried it on my site and basically nothing happened. Any ideas why?

    Reply
    • Paal Joachim says

      13 April - 2017

      Hey Simeon

      I have not checked it in a while. I really do not know why it is not working.
      Do a search and see what yo come up with. I will have to check my various notes and go through it again.
      Thanks for checking it out! I will look into it.

      Happy Easter!

      Reply

Leave a Comment Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2021 · By Easy Web Design Tutorials · Built on the Genesis Framework · WordPress · Log in · ⇪

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.