0

I am new to Wordpress development. I had to apply a filter to add a custom HTML attribute to the image fields. I managed to add a custom input field to the media uploader and store the metadata in the database. The problem I am facing is while retrieving the information.

I tried two different filters, get_image_tag & wp_get_attachment_image_attributes filters but both work strangely.

1) wp_get_attachment_image_attributes filter adds the html attribute, but only to featured images and not regular images. I would love if I could make it work with regular blog post images too.

Why doesn't wp_get_attachment_image_attributes work for regular blog post images? It is not mentioned anywhere in the docs that the filter is only meant for featured images.

2) get_image_tag filter would only work for new images I upload & not the old ones so I don't prefer it but as wp_get_attachment_image_attributes didn't work properly, I gave it a try. I have the following code:

add_filter( 'get_image_tag', 'wpse_235266_image_html', 10, 6 );

function wpse_235266_image_html( $html, $id, $alt, $title, $align, $size ) {

    $html = str_replace( '<img ', "<img data='b' ", $html );
    return $html;

}

Now after hit & trial, I found that the above works only if I replace data with data-X where X is any string.

Why does the HTML attribute need to have a hyphen?

0

Featured images are stored separately in the database and retrieved with a separate function. So it is possible to attach a filter to that function. Regular images are just strings in the post field or in a widget. You can still filter those, but it takes more effort. The filter you built on get_image_tag can also be used on the_content (for posts and pages) and widget_text (for the text widget).

Beware that if you have already used the filter on get_image_tag, your data attribute is already included as a string in the post, so if you use the filter again it will be included twice, unless you include a test for its presence.

As for the hyphen in the data attribute, that's simply how the attribute is defined. Just data is invalid html.

3
  • I am sorry but I still don't undestand why wp_get_attachment_image_attributes is only working for featured images and not regular post images? I mean the filter name says get attachment image attributes. It doesn't say it is only for featured images
    – Anon
    Jul 7 '18 at 12:40
  • It should work the moment you insert the image in the post. But once it is a html string in the post the function isn't called anymore. You can work around this by including images with the gallery shortcode.
    – cjbj
    Jul 7 '18 at 12:50
  • "It should work the moment you insert the image in the post", you're talking about the get_image_tag while I am asking about wp_get_attachment_image_attributes in my previous comment. wp_get_attachment_image_attributes is called everytime a featured image is displayed on my blog, it just doesn't get called for regular blog post images.
    – Anon
    Jul 7 '18 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.