add data-attribute to all images inside the_content()

I wonder if it’s possible to add a filter to all images inside the_content() to create the following image pattern for all images …

<img class="digest" src="smallest-file.jpg" data-fullsrc="largest-file.jpg" alt="something"/>

So I’d like to add a class="digest" to the img tag. Additionally the src attribute of the image should always link to the smallest-file wordpress creates and a data-fullsrc that links to the largest file of the image.

Read More

Any idea or approach on how to do this?
I’d really appreciate your help with this as I’m completely helpless right now.

Related posts

Leave a Reply

1 comment

  1. You will need to take a look at the image_send_to_editor filter.
    This will not update existing records but it if you get it working it will apply to each newly inserted image.

    very basic filter:

    function give_linked_images_class($html, $id, $caption, $title, $align, $url, $size, $alt = '' ){
        $html; //contains the string you need to edit, you might want to consider to rebuild the complete string.
    
        return $html;
    }
    add_filter('image_send_to_editor','give_linked_images_class',10,8);