Automatically setting the featured image without a plugin
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.
Hey Paal,
Is this still valid for the newest WP version? I tried it on my site and basically nothing happened. Any ideas why?
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!