Skip Navigation

[Resolved] Adding Class to Img Tag / Custom Image Size Not Loading via Shortcode

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user is using a lazy loading plugin to optimize the loading of images, he needs to add a specific class to the tag generated by the Image block.
The classes added to the image block settings are added in the

tag that wraps the tag.

Solution:
We can work around this using custom code:

function add_class_to_image_block( $block_content, $block ) {
    if ( $block['blockName'] === 'toolset-blocks/image' ) {
        return str_replace( '<img src', '<img class="my-class" src', $block_content );
    }
 
    return $block_content;
}
 
add_filter( 'render_block', 'add_class_to_image_block', 10, 2 );

Relevant Documentation:
https://developer.wordpress.org/reference/hooks/render_block/

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 5 replies, has 2 voices.

Last updated by JoelK2744 3 years, 10 months ago.

Assisted by: Jamal.

Author
Posts
#1983467

Hello, I have a performance plugin with lazy loading feature , the feature doesn't work particularly well on one of my views so I would like to exclude these images, see - hidden link

I've tried adding the class to exclude lazy loading as described above to the toolset image block but the class loads on the "figure" tag not the actual "img" tag - is there any way of adding the class to the "img" tag via blocks?

As an alternative, I've tried using a shortcode instead, this does add the class correctly but it doesn't seem to load my custom image size correctly which I have registered and is available to select if I use the Toolset Image Block. I presume the size should be the slug like "medium" "medium_large" etc but this doesn't seem to work with my registered custom size [wpv-post-featured-image size="my-custom-image-size" class="my-class"]

Thanks

#1983941

...sorry please ignore the bit about the shortcode, I've worked out why that wasn't loading the custom image size. However I would still like to know if it is possible to add a class to the img tag using the Toolset Image Block.

Thanks

#1983969

Hello and thank you for contacting the Toolset support.

I believe that you can use the render_block filter to change the output of the block. You can use the str_replace function in PHP to replace the image tag by adding a class. For example: replace '<img' with '<img class="the-class"'. Does it make sense?
- hidden link

#1983997

Ok thanks Jamal...PHP isn't my strong point but if that's the only way I'll look into it

thanks

#1984967

I run a test locally and I come up with the following code. Please note that it will be executed for all Toolset Image blocks.

function add_class_to_image_block( $block_content, $block ) {
	if ( $block['blockName'] === 'toolset-blocks/image' ) {
		return str_replace( '<img src', '<img class="my-class" src', $block_content );
	}

	return $block_content;
}

add_filter( 'render_block', 'add_class_to_image_block', 10, 2 );

Check the result in this screenshot hidden link

#1985263

Thanks Jamal, much appreciated.