Skip to content
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

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
67 changes: 67 additions & 0 deletions fix-block-img-tag-sizes.php
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) {
Copy link
Member

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 to true anywhere.

Copy link
Contributor Author

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).

// 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) . '">',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this foo="bar" thing really necessary? :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, can the <img> tag contain other attributes (say, alt)? If so, is it possible to avoid overwriting them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@itsTallulah itsTallulah Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the first commit: it has the now-removed add_block_bits, which I was using for as an informal test for extra values in the tags by filtering a bunch of extra stuff into the block markup just upstream.

$block_content
);
}
return $block_content;
}
add_filter('render_block', 'fix_img_block_sizes', 10, 3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're only handling core/image blocks, perhaps we can hook into render_block_core/image here? https://github.com/WordPress/wordpress-develop/blob/50af37a9083f003f8e98d089091d2cc428797cc5/src/wp-includes/class-wp-block.php#L581

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well and makes the function smaller. Nice, thx!

Loading