A lot of plugins use the Featured Image feature for showing post thumbnails. My Better Recent Posts Widget Pro is a perfect example. It uses the Featured Image to display small thumbnails of the recent posts in widget areas. There’s one problem though. This feature only works if the currently active theme supports post thumbnails. In my opinion, every theme, no matter what, should support post thumbnails. It’s simply too useful of a feature to not support, but some don’t. So here’s how to check if a theme does support post thumbnails, and add support for them if not already supported.

There’s a simple function we can use called current_theme_supports(). It will allow us to determine if the current theme has included support for certain features. In this case, we’re going to check for support of post-thumbnails. It works like this:

1
2
3
4
5
6
function pippin_add_thumbnail_support() {
	if(!current_theme_supports('post-thumbnails')) {
		// add support here
	}
}
add_action('init', 'pippin_add_thumbnail_support');

So now to also add support for post thumbnails (if it doesn’t exist), we do this:

1
2
3
4
5
6
function pippin_add_thumbnail_support() {
	if(!current_theme_supports('post-thumbnails')) {
		add_theme_support('post-thumbnails');
	}
}
add_action('init', 'pippin_add_thumbnail_support');

This is an important step to remember when writing plugins that use the post thumbnails. If you don’t check for support, and you use any function such as has_post_thumbnail(), a fatal error will be thrown if the theme doesn’t support post thumbnails.

  1. Edward

    Would it be better to hook this into after_setup_theme rather than init?

    • Pippin

      Yes, that would be better.

Comments are closed.