Change html structure of all img tags in WordPress
-
I have a WordPress blog and am trying to implement the foresight.js image script. In short, I need to target all post images, swap out the
src, width, & height
attributes withdata-src, data-width, & data-height
attributes. I then need to duplicate the image line and wrap it in<noscript>
tags. This is the structure I’m trying to have WordPress generate/create:<img data-src="wordpress/image/url/pic.jpg" data-width="{get width of image with PHP & pass-in that value here} data-height="{get height of image with PHP and pass-in that value here}" class="fs-img"> <noscript> <img src="wordpress/image/url/pic.jpg"> </noscript>
More info can be found at the foresight.js website.
I have searched the WordPress codex and the best possible route I can find are to use filters (ie. ‘get_image_tag’ & ‘image_tag’) for modifying the html that WordPress outputs for each image. I’m thinking that one of these options should work, or that I can do some pattern matching with regex (I know, not ideal), throw in a
preg_replace
and then inject this back intothe_content
filter.I have tried some of these options but cannot get any to work. Found some suggestions here, here, and sort of here, but can’t even get any of them to work! Could someone please offer some help?
‘get_image_tag’ attempt:
Found this particular one on the web, but it would need modified to fit my logic (see above structure)…can’t make sense of what thepreg_replace
array is doing on my own.<?php function image_tag($html, $id, $alt, $title) { return preg_replace(array( '/'.str_replace('//','\/\/',get_bloginfo('url')).'/i', '/\s+width="\d+"/i', '/\s+height="\d+"/i', '/alt=""/i' ), array( '', '', '', alt='"' . $title . '"' ), $html); } add_filter('get_image_tag', 'image_tag', 0, 4); ?>
Another ‘get_image_tag’ attempt:
<?php function get_image_tag($id, $alt, $title, $align, $size='full') { list($width, $height, $type, $attr) = getimagesize($img_src); $hwstring = image_hwstring($width, $height); $class = 'align' . esc_attr($align) . ' size-' . esc_attr($size) . ' wp-image-' . $id; $class = apply_filters('get_image_tag_class', $class, $id, $align, $size); $html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" title="' . esc_attr($title).'" data-width="' . $width . '" data-height="' . $height . '" class="' . $class . ' fs-img" />'; $html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size); return $html; } ?>
Pattern-matching attempt:
Tried creating my own regex on this one, but not sure if it’s correct.<?php function restructure_imgs($content) { global $post; $pattern = "/<img(.*?)src=('|\")(.*?).(bmp|gif|jpeg|jpg|png)(|\")(.*?)>/i"; list($width, $height, $type, $attr) = getimagesize($2$3.$4$5); $hwstring = image_hwstring($width, $height); $replacement = '<img$1data-src=$2$3.$4$5 title="'.$post->post_title.'" data-width="'.$width.'" data-height="'.$height.'" class="fs-img"$6>'; $content = preg_replace($pattern, $replacement, $content); return $content; } add_filter('the_content', 'restructure_imgs'); ?>
Unfortunately can’t get any of these examples to work. Any help or sharing your pre-written scripts/functions would be much appreciated! Thanks for helping a student learn!!
- The topic ‘Change html structure of all img tags in WordPress’ is closed to new replies.