How do you tell WordPress to automatically add a custom class to each image being posted in the editor?

Every time I add a post with images it turns them into a block with a figure element around my image. Then I click edit as HTML, and add a “center-block” to my image.

I have to repeat this step for every single image in the post. Is there a way for WordPress to do this automatically? Or is there a different method to apply the center-block class to images in my post?

I had asked ChatGPT and it came up with the following code which doesn’t seem to do anything:

`function add_image_class($class){
$class .= ‘ custom-class’;
return $class;
}
add_filter(‘get_image_tag_class’,’add_image_class’);`

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer