-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add srcset
and sizes
in content with a filter
#177
Conversation
This changes our approach from one where `srcset` and `sizes` are written to the image markup in post content to one where the responsive attributes are added on the front end when the page is being rendered. There is a small performance hit but the benefit is worth the tradeoff for what is gained. What exactly do we gain? Glad you asked: * Automatically extend support to images in posts published before we responsive image support was available. * Removes the need to update the markup in TinyMCE when the image size is changed. * Keeps `sizes` attributes, which should probably be adjusted based on the needs of the theme, from being stored in the database. * Is non-destructive if a site needs to change available image sizes and regenerate thumbnails. Changes: * Removes the old JS code for handling markup changes in TinyMCE * Adds `tevkori_filter_content_images()` as a display filter. * Adds `_tevkori_filter_content_images_callback()` to process filtered images. * Adds tests
63f5156
to
84bc9ee
Compare
I replaced the |
Add `srcset` and `sizes` in content with a filter
$uploads_dir = wp_upload_dir(); | ||
$path_to_upload_dir = $uploads_dir['baseurl']; | ||
|
||
$content = preg_replace_callback( '|<img ([^>]+' . $path_to_upload_dir . '[^>]+)[\s?][\/?]>|i', '_tevkori_filter_content_images_callback', $content ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure but when I look at this regex it appears to me that the first group will contain everything up to the first >
that it finds. So if there is a space and slash before the closing tag, it will be included.
Shouldn't we remove [\s?][\/?]
and then change line 418 from
$image_html = "<img " . $atts . " " . $srcset . " " . $sizes . " />";
to
$image_html = "<img $srcset . " " . $sizes . " " . $atts . ">";
?
The space and the slash will be part of $atts
.
Or am I wrong?
This changes our approach from one where
srcset
andsizes
are written to theimage markup in post content to one where the responsive attributes are added on the
front end when the page is being rendered. There is a small performance hit but the
benefit is worth the tradeoff for what is gained. What exactly do we gain? Glad you
asked:
support was available.
sizes
attributes, which should probably be adjusted based on the needs of thetheme, from being stored in the database.
thumbnails.
Changes:
tevkori_filter_content_images()
as a display filter._tevkori_filter_content_images_callback()
to process filtered images.Props @jaspermdegroot, @peterwilsoncc
Related: #170
Replaces: #175
Resolves: #83