Add custom class on images - WordPress

When we attach & insert an image in to WordPress post / page, the WordPress will add some default classes to that image.

In some situations, I want to add custom class to all images on WordPress site. To do so, we can use a filter function.

function add_custom_image_class($class) {
	$class .= ' my-custom-class';
	return $class;
}
add_filter('get_image_tag_class', 'add_custom_image_class' );

If we want to add custom class on specific images like thumbnails only, there is option to pass class name as parameter in such function call.

the_post_thumbnail('full', array( 'class'	=> "my-custom-class my-custom-class-2"));

Need Help?

Do you need any help on WordPress? I'm happy to help you.

Contact Me

Recent Posts

  1. Update query order - WordPress
  2. Display featured products - WooCommerce
  3. Remove "Category:", "Tag:", "Author:" from the_archive_title - WordPress
  4. The essential & basic WP-CLI commands for a WordPress developer
  5. Install theme unit test data using WP-CLI - WordPress


Questions / Comments

If you found this article interesting, found errors, or just want to discuss about them, please get in touch.