1

Is it possible to add an attribute and value to image links?

I currently have:

<a href="#">
  <img class="img-responsive aligncenter wp-image-1286 size-full" src="#" alt="..." width="858" height="304">
</a>

and would like to add data-rel="lightbox-0" to the link:

<a href="#" data-rel="lightbox-0">
  <img class="img-responsive aligncenter wp-image-1286 size-full" src="#" alt="..." width="858" height="304">
</a>

I also need to add the new attribute to images in a specific taxonomy but I think I can figure out that part.

2

while googling about the same issue I came upon your question and a similar question from the Wordpress forums. You can control the output of the generated code via the image_send_to_editor filter like this:

function filter_image_send_to_editor($html, $id, $caption, $title, $align, $url, $size, $alt) {
  $html = sprintf('<a href="#" data-rel="lightbox-0"><img alt="%2$s" src="%1$s" /></a>', esc_attr(esc_url($url)), esc_attr($title));

  return $html;
}

add_filter('image_send_to_editor', 'filter_image_send_to_editor', 10, 8);

Original link: https://wordpress.org/support/topic/change-generated-html-code-from-add-media-button

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.