-
Notifications
You must be signed in to change notification settings - Fork 101
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
Fix/block img tag sizes #5776
base: develop
Are you sure you want to change the base?
Fix/block img tag sizes #5776
Changes from 2 commits
44b0945
ca09fe7
bbd458b
174a74b
33a0d47
ec2b0f6
77accab
a7b1702
06865ff
c59dd9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
// Adds 'correct' w & h values to img tags in core/image blocks | ||
|
||
function fix_img_block_sizes($block_content, $block, $instance) | ||
{ | ||
|
||
// Use resize args for images instead of w|h|crop (optional) | ||
$resizearg = false; | ||
|
||
// Don't fire in wp-admin | ||
if (!is_admin()) { | ||
|
||
if ('core/image' !== $block['blockName']) { | ||
return $block_content; // Only modify core/image blocks | ||
} | ||
|
||
// Extract the image ID from the wp-image-* class within the img tag | ||
if (preg_match('/<img[^>]* class="[^"]*wp-image-(\d+)[^"]*"/', $block_content, $matches)) { | ||
$image_id = $matches[1]; | ||
} else { | ||
return $block_content; // No image ID found, return original content | ||
} | ||
|
||
// Extract the size class from the figure tag's class attribute | ||
if (preg_match('/<figure[^>]* class="[^"]*size-([^\s"]+)[^"]*"/', $block_content, $size_matches)) { | ||
$size_name = $size_matches[1]; | ||
} else { | ||
return $block_content; // No size found, return original content | ||
} | ||
|
||
// Get the image metadata | ||
$metadata = wp_get_attachment_metadata($image_id); | ||
if (!$metadata || !isset($metadata['sizes'][$size_name])) { | ||
return $block_content; // No metadata found for this size, return original content | ||
} | ||
|
||
// Get the width, height, and file from the metadata | ||
$width = $metadata['sizes'][$size_name]['width']; | ||
$height = $metadata['sizes'][$size_name]['height']; | ||
$new_file = $metadata['sizes'][$size_name]['file']; | ||
|
||
if ($resizearg) { | ||
// Get the original image URL and replace the file name with the new file | ||
if (preg_match('/<img [^>]*src="([^"]+)"[^>]*>/', $block_content, $url_matches)) { | ||
$original_url = $url_matches[1]; | ||
$parsed_url = wp_parse_url($original_url); | ||
$base_url = trailingslashit($parsed_url['scheme'] . '://' . $parsed_url['host'] . dirname($parsed_url['path'])); | ||
|
||
// Construct new URL with the new file | ||
$new_url = $base_url . $new_file; | ||
|
||
// Replace the original URL with the new URL in the img tag | ||
$block_content = str_replace($original_url, $new_url, $block_content); | ||
} | ||
} | ||
|
||
// Add the width and height attributes to the img tag | ||
$block_content = preg_replace( | ||
'/<img ([^>]+)>/', | ||
'<img foo="bar" $1 width="' . esc_attr($width) . '" height="' . esc_attr($height) . '">', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, can the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, that foo is a testing leftover! Thanks for the spot. And yeah, the img and figure tags can contain other values and classes too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the first commit: it has the now-removed |
||
$block_content | ||
); | ||
} | ||
return $block_content; | ||
} | ||
add_filter('render_block', 'fix_img_block_sizes', 10, 3); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're only handling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point - Should behave exactly the same on that filter, since it's right after the first one and uses the same values. Will give that a try too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works well and makes the function smaller. Nice, thx! |
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.
This condition is always
false
because we don't seem to set$resizearg
totrue
anywhere.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.
Indeed, we don't set it true anywhere - yet. I'm deferring to y'all as to how we should let the user set this (if we include it at all).