Seting the default featured image using wordpress plugin - Default featured image

Setting default featured image using wordpress plugin

Many people using featured images in there blog or sites. Setting default featured image using wordpress plugin for every post is not possible for some times so creating the one default image and set that default image for all the posts is really good idea. Some times If you not have the default image then it is possible to break your wordpress theme or it does not look nice without featured image. In this article I will show you how to Setting default featured image using wordpress plugin.

 

Setting default featured image using wordpress plugin

Setting default featured image using wordpress plugin for post is not possible for some times so creating default image, for all posts is really good idea.

But still you want to set the default featured image using following wordpress plugin.

Default featured image
http://wordpress.org/plugins/default-featured-image/

Add a default featured image to the media settings page. This featured image will show up if no featured image is set. Simple as that.

For exceptions and to see which functions to use see the FAQ.
My chosen featured image doesn’t show, why isn’t it working?

This plugin can’t guarantee that it works. That depends on the themes. Still I want to know if it fails, so contact me
Which functions can I use to display the featured image?

The plugin uses the default WordPress functions the_post_thumbnail or get_the_post_thumbnail. has_post_thumbnail will always return true. get_post_thumbnail_id will return the ID set on the post or the DFI you set.
Can I exclude a page or give it a different image?

yes. you can exclude all kinds of things with the conditional tags. A few examples which you can paste in your functions.php

Dont use a featured image on page 5


function dfi_skip_page( $dfi_id ) {
 if ( is_single( 5 ) || get_the_ID() == 5 ) {
 return 0; // invalid id
 }
 return $dfi_id; // the original featured image id
 }
 add_filter('dfi_thumbnail_id', 'dfi_skip_page' );

Use a different image on the “book” posttype. The ID of the image is 12


function dfi_posttype_book( $dfi_id ) {
 if ( is_singular( 'book' ) || get_post_type() == 'book' ) {
 return 12; // the image id
 }
 return $dfi_id; // the original featured image id
 }
 add_filter('dfi_thumbnail_id', 'dfi_posttype_book' );

Use a different image on certain categories


function dfi_category( $dfi_id ) {
 if ( has_category( 'category-slug' ) ) {
 return 13; // the image id
 } else if ( has_category( 'other_category' ) ) {
 return 14; // the image id
 }
 return $dfi_id; // the original featured image id
 }
 add_filter('dfi_thumbnail_id', 'dfi_category' );

Can I change the HTML of the image returned?

yes you can with the filter dfi_thumbnail_html.


function dfi_add_class($html, $post_id, $default_thumbnail_id, $size, $attr) {
 // add a class to the existing class list
 $attr['class'] .= ' my-class';

return wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr );
 }
 add_filter( 'dfi_thumbnail_html', 'dfi_add_class', 10, 5 );

First thing you need to do is install and activate the Default Featured Image plugin. Upon activation, the plugin adds an option under Setttings » Media to choose a default image.

Setting default featured image using wordpress plugin
Setting default featured image using wordpress plugin

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

2 thoughts on “Setting default featured image using wordpress plugin”

  1. The featured image on page is not working for me.
    My page is not single, I have a loop. My news page is ID 12. Any ideas?

Leave a Reply to Purab Kharat Cancel reply

Your email address will not be published.