Make WordPress Core

Opened 4 months ago

Closed 10 hours ago

Last modified 10 hours ago

#63714 closed defect (bug) (fixed)

Missing width/height attributes on SVG file since WP 6.8.2

Reported by: rainbowgeek's profile rainbowgeek Owned by: adamsilverstein's profile adamsilverstein
Milestone: 6.9 Priority: normal
Severity: normal Version: 6.8.2
Component: Media Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

The latest release of WP (6.8.2) removes all the width and height attributes on SVG files causing massive design issues.

See attachment.

Downgrading to 6.8.1 solves the bug.

Attachments (2)

with-attributes.png (91.7 KB) - added by rainbowgeek 4 months ago.
WP 6.8.1 with attributes
without-attributes.png (47.7 KB) - added by rainbowgeek 4 months ago.
WP 6.8.2, missing attributes

Download all attachments as: .zip

Change History (43)

@rainbowgeek
4 months ago

WP 6.8.1 with attributes

@rainbowgeek
4 months ago

WP 6.8.2, missing attributes

#1 follow-up: @rainbowgeek
4 months ago

Code used:

wp_get_attachment_image($image, $size, '', array('class' => 'mb-4', 'height' => 60, 'width' => 60));

#2 @ocean90
4 months ago

  • Component changed from General to Media

Hello @rainbowgeek, welcome to WordPress Trac!

Thanks for the report. The only related change seems to be [60415]. Do you mabye have a plugin active which changes the attributes via the wp_get_attachment_image_attributes filter?

#3 @rainbowgeek
4 months ago

We confirm that no plugins, themes, or must-use plugins are using the wp_get_attachment_image_attributes hook.

#4 @audrasjb
4 months ago

  • Milestone changed from Awaiting Review to 6.8.3

Moving to 6.8.3 for visibility

#5 in reply to: ↑ 1 @rollybueno
4 months ago

First look, I don't think $attr supports 'height' & 'width' as per https://developer.wordpress.org/reference/functions/wp_get_attachment_image/

Though the doc could be outdated.. will try to reproduce later.

Replying to rainbowgeek:

Code used:

wp_get_attachment_image($image, $size, '', array('class' => 'mb-4', 'height' => 60, 'width' => 60));

#6 @rollybueno
4 months ago

Reproduction Report

Description

This report validates whether the issue can be reproduced.

Environment

  • WordPress: 6.8.1
  • PHP: 8.2.27
  • Server: nginx/1.26.1
  • Database: mysqli (Server: 8.0.35 / Client: mysqlnd 8.2.27)
  • Browser: Chrome 137.0.0.0
  • OS: Linux
  • Theme: Twenty Fifteen 4.0
  • MU Plugins: None activated
  • Plugins:
    • Safe SVG 2.3.1
    • Test Reports 1.2.0

Actual Results

  1. ❎ Cannot reproduce.

I came to this conclusion since the report stated that the code used wp_get_attachment_image($image, $size, '', array('class' => 'mb-4', 'height' => 60, 'width' => 60)); is working on WP v6.8.1.

After using the same code with correct SVG attachment ID and empty size parameters, the wp_get_attachment_image() ignores the height & width in the $attr params even in 6.8.1.

Additional Notes

  • Downgraded to 6.8.1
  • Uses 2015 theme with only 2 plugins active
  • There's no additional filter manipulation, only the code provided
  • Uses same code provided by reporter echo wp_get_attachment_image( 8, '', '', array( 'class' => 'mb-4', 'height' => 60, 'width' => 60 ) );
  • The only difference are the 1st & 2nd parameters which are ID and size.
  • The WebP version ignores the height & width value but returns srcset for smaller device compatibility.
  • The SVG ignores the height and width

❗❗❗ The only time I was able to change the height and width value through wp_get_attachment_image() is when using array( 60, 60 ) in the $size param, but it only works on normal images and not SVG.
Code used: https://i.imgur.com/r9FGUoI.png

Result: https://i.imgur.com/UBfteHE.png

❗❗❗We need more tester to try reproducing

Supplemental Artifacts

Code used: https://i.imgur.com/KJwsytc.png

6.8.1

WebP:
Backend: Confirming we have WP v6.8.1
https://i.imgur.com/d6pO5aw.png
Frontend:
https://i.imgur.com/LOqrQGa.png

SVG:
Backend:
https://i.imgur.com/OFgKLXQ.png
Frontend:
https://i.imgur.com/8dpCn77.png

#7 follow-up: @rainbowgeek
4 months ago

Use this code to allow SVG in a mu-plugin (not Safe SVG plugin):

function wp_mime_types($mimes) {
	$mimes['svg'] = 'image/svg+xml';
	return $mimes;
}
add_filter('upload_mimes', 'wp_mime_types');

Then upload a SVG file to the media library, pass the attachment ID to the $image variable:

wp_get_attachment_image(1, 'full', '', array('class' => 'mb-4', 'height' => 60, 'width' => 60));

Echo it in your theme somewhere (eg: functions.php with the code below).

 add_action('wp_head', function() {
	echo wp_get_attachment_image(7, 'full', '', array('class' => 'mb-4', 'height' => 60, 'width' => 60));
 });

Result with WP 6.8.2:

<img src="http://test.local/wp-content/uploads/2025/07/ico-newsletter.svg" class="mb-4" alt="" decoding="async" loading="lazy">

Result with WP 6.8.1:

<img src="http://test.local/wp-content/uploads/2025/07/ico-newsletter.svg" class="mb-4" alt="" height="60" width="60" decoding="async" loading="lazy">
Last edited 4 months ago by rainbowgeek (previous) (diff)

#8 @shreya0shrivastava
4 months ago

I was able to reproduce the bug following the steps mentioned by @rainbowgeek

Reproduction Report

Description

This report validates whether the issue can be reproduced.

Environment

  • WordPress: 6.8.1, 6.8.2
  • PHP: 8.2.23
  • Server: nginx/1.26.1
  • Database: mysqli (Server: 8.0.35 / Client: mysqlnd 8.2.23)
  • Browser: Chrome 138.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.0
  • MU Plugins:
    • test 1 (contains code to allow SVG)
  • Plugins:
    • Test Reports 1.2.0

Actual Results

  1. ✅ Error condition occurs (reproduced).

Steps for reproduction

  1. Add a mu-plugin with the following code:
    <?php
    add_filter('upload_mimes', function($mimes) {
           $mimes['svg'] = 'image/svg+xml';
           return $mimes;
       });
    
  1. Go to the Media Library and upload an SVG file. Note its attachment ID.
  1. In your theme’s functions.php, add:
<?php
add_action('wp_head', function() {
       echo wp_get_attachment_image(ATTACHMENT_ID, 'full', '', array(
           'class' => 'mb-4',
           'height' => 240,
           'width'  => 240
       ));
   });

Replace ATTACHMENT_ID with the actual SVG attachment ID.

  1. Visit the frontend and inspect the rendered <img> tag in the browser.
  2. Check for both WP 6.8.1 and WP 6.8.2

Additional Notes

  • Height and width attributes show up on using WP 6.8.1
  • Height and width attributes do not show up on using WP 6.8.2
  • The same code was used to add SVG for both WP versions

Supplemental Artifacts

For WP 6.8.1
https://i.ibb.co/9kWHbXzK/Screenshot-2025-07-22-at-4-11-36-PM.png

For WP 6.8.2
https://i.ibb.co/Rp3464cR/Screenshot-2025-07-22-at-4-17-43-PM.png

#9 @heybran
4 months ago

We had same issue in some of the sites we're trying to update today. Still trying to see if we can find a way to work around this, meaning to get the width and height attributes back on SVG logos without rolling back to WordPress 6.8.1.

We're rendering the SVG images with wp_get_attachment_image() call.

echo wp_get_attachment_image(
	$logo,
	'logo-235x40',
	false,
	array(
		'alt'    => esc_attr( get_bloginfo( 'name' ) . ' - ' . get_bloginfo( 'description' ) ),
		'height' => '40',
		'width'  => '235',
	)
);

#10 in reply to: ↑ 7 @rollybueno
4 months ago

  • Keywords needs-patch added

Thank you! This filter upload_mimes is the missing piece to reproduce, and I was able to do that after adding that filter.

function wp_mime_types($mimes) {
	$mimes['svg'] = 'image/svg+xml';
	return $mimes;
}
add_filter('upload_mimes', 'wp_mime_types');

6.8.1:
https://i.imgur.com/CAU1xPr.png

6.8.2
https://i.imgur.com/lWFhq5B.png

Replying to rainbowgeek:

Use this code to allow SVG in a mu-plugin (not Safe SVG plugin):

function wp_mime_types($mimes) {
	$mimes['svg'] = 'image/svg+xml';
	return $mimes;
}
add_filter('upload_mimes', 'wp_mime_types');

Then upload a SVG file to the media library, pass the attachment ID to the $image variable:

wp_get_attachment_image(1, 'full', '', array('class' => 'mb-4', 'height' => 60, 'width' => 60));

Echo it in your theme somewhere (eg: functions.php with the code below).

 add_action('wp_head', function() {
	echo wp_get_attachment_image(7, 'full', '', array('class' => 'mb-4', 'height' => 60, 'width' => 60));
 });

Result with WP 6.8.2:

<img src="http://test.local/wp-content/uploads/2025/07/ico-newsletter.svg" class="mb-4" alt="" decoding="async" loading="lazy">

Result with WP 6.8.1:

<img src="http://test.local/wp-content/uploads/2025/07/ico-newsletter.svg" class="mb-4" alt="" height="60" width="60" decoding="async" loading="lazy">

#11 @rollybueno
4 months ago

  • Keywords 2nd-opinion dev-feedback added

Run few more test, the point of this regression(not quite sure if this should be treated as such) happens in this line: https://core.trac.wordpress.org/browser/tags/6.8.2/src/wp-includes/media.php#L1094 where it completely overrides the value of height and width set when using wp_get_attachment_image() e.g.

wp_get_attachment_image(1, 'full', '', array('class' => 'mb-4', 'height' => 60, 'width' => 60));

The $width and $height is coming from the shorthand values of wp_get_attachment_image_src().

6.8.1 set it first on temporary variable as per https://core.trac.wordpress.org/browser/tags/6.8.1/src/wp-includes/media.php#L1096 before using it later on, instead of overriding those values by default.

I traced this back to this commit - https://github.com/WordPress/wordpress-develop/commit/4014d16d222 from https://core.trac.wordpress.org/ticket/14110, which was added just 2 weeks ago. It basically, prioritising the width and height through wp_get_attachment_image_attributes filter.

With that, I think it's either control the height & width in that filter, or do some backward compatibility.

function test_wp_get_attachment_image_attributes( $attr, $attachment, $size ) {
	// Example: Only target SVGs
	$mime = get_post_mime_type( $attachment );
	if ( $mime === 'image/svg+xml' ) {
		// Set custom width and height
		$attr['width']  = 60;
		$attr['height'] = 60;
	}
	return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'test_wp_get_attachment_image_attributes', 10, 3 );

60x60
https://i.imgur.com/KUXLXVv.png

function test_wp_get_attachment_image_attributes( $attr, $attachment, $size ) {
	// Example: Only target SVGs
	$mime = get_post_mime_type( $attachment );
	if ( $mime === 'image/svg+xml' ) {
		// Set custom width and height
		$attr['width']  = 150;
		$attr['height'] = 150;
	}
	return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'test_wp_get_attachment_image_attributes', 10, 3 );

150x150
https://i.imgur.com/goF55oH.png

Adding 2nd-opinion tag to get more opinions if we should:

  1. Keep the current version & control the height & width through wp_get_attachment_image_src filter or
  2. Introduce a backward compatibility..

#12 follow-up: @rollybueno
4 months ago

  • Keywords needs-patch removed

This ticket was mentioned in PR #9315 on WordPress/wordpress-develop by @heybran.


4 months ago
#13

  • Keywords has-patch has-unit-tests added

#14 in reply to: ↑ 12 ; follow-up: @heybran
4 months ago

Replying to rollybueno:

Hi Rolly,

I noticed that you've removed the needs-patch keyword, does it mean this ticket is not ready for a PR/patch? I just submitted a PR to fix the issue reported in this ticket. First time contributing codes, so please forgive me if this question is a bit stupid.

@mukesh27 commented on PR #9315:


4 months ago
#15

Thanks @heybran for the PR.

I’ve left some feedback and suggestions. Please take a look and let me know if you need any clarification or further details.

@mukesh27 commented on PR #9315:


4 months ago
#16

@adamsilverstein Could you please take a look when you have time?

#17 in reply to: ↑ 14 @rollybueno
4 months ago

  • Keywords needs-testing added; 2nd-opinion dev-feedback removed

Hey @heybran, no not at all. The reason I removed that is for further discussions with the maintainer, but since you already push a PR, let's go ahead. Mukesh already left some review, which is great.

Replying to heybran:

Replying to rollybueno:

Hi Rolly,

I noticed that you've removed the needs-patch keyword, does it mean this ticket is not ready for a PR/patch? I just submitted a PR to fix the issue reported in this ticket. First time contributing codes, so please forgive me if this question is a bit stupid.

#18 @rollybueno
4 months ago

Test Report

Description

This report validates whether the indicated patch works as expected.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/9315

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.28
  • Server: nginx/1.29.0
  • Database: mysqli (Server: 8.4.5 / Client: mysqlnd 8.2.28)
  • Browser: Chrome 137.0.0.0
  • OS: Linux
  • Theme: Twenty Fifteen 4.0
  • MU Plugins: None activated
  • Plugins:
    • Query Monitor 3.18.0
    • Test Reports 1.2.0

Actual Results

✅ - Tested using defined width and height on attr params in wp_get_attachment_image.
✅ - Tested without width and height on attr params in wp_get_attachment_image. It uses the attachment height and width by default. No regressions found.
✅ - Tested setting width and height through wp_get_attachment_image_attributes filter. Confirming that the enhancement on https://core.trac.wordpress.org/ticket/14110 will not regress.
✅ - Tested setting width and height through wp_get_attachment_image_attributes filter and through attr params in wp_get_attachment_image to see which one will be prioritised. Height and width values are coming from wp_get_attachment_image_attributes filter, which is correct since it comes late in heirarchy. No regressions found.

✅ Issue resolved with patch.

Additional Notes

  • To pull the PR, follow these steps:
    git fetch upstream pull/9315/head:pr-9315
    git checkout pr-9315
    
  • Add the following filter on your theme's functions.php
    add_filter(
    	'upload_mimes',
    	function ( $mimes ) {
    		$mimes['svg'] = 'image/svg+xml';
    		return $mimes;
    	}
    );
    
  • To set the widht and height through wp_get_attachment_image_attributes filter, use this on your theme functions.php
    function test_wp_get_attachment_image_attributes( $attr, $attachment, $size ) {
    	// Example: Only target SVGs
    	$mime = get_post_mime_type( $attachment );
    	if ( $mime === 'image/svg+xml' ) {
    		// Set custom width and height
    		$attr['width']  = 150;
    		$attr['height'] = 150;
    	}
    	return $attr;
    }
    add_filter( 'wp_get_attachment_image_attributes', 'test_wp_get_attachment_image_attributes', 10, 3 );
    
  • To run the test unit:
    npm run test:php -- --filter test_wp_get_attachment_image_not_overwrite_user_provided_width_height
    
  • Use the following code, which I put in the content.php using 2015 theme, where 70 is the SVG attachment ID in my local. Replace that based in your SVG attachment ID in your local env:
    echo wp_get_attachment_image( 70, 'full', '', array( 'class' => 'mb-4', 'height' => 60, 'width' => 60 ) );
    

Supplemental Artifacts

See the before and reproduced information on https://core.trac.wordpress.org/ticket/63714#comment:11

After pulling the patch and restarting the env:

This now respecting the user defined height and width when using wp_get_attachment_image()
https://i.imgur.com/XkWWbmB.png

Test unit:
https://i.imgur.com/HJIKtaS.png

#19 @rollybueno
4 months ago

So far, the patch from @heybran is okay. I'll leave the needs-testing open a bit for at least 1 more result from other tester, and we wait for Component Maintainer to review and approve.

cc @adamsilverstein

Last edited 4 months ago by rollybueno (previous) (diff)

#20 @adamsilverstein
4 months ago

  • Owner set to adamsilverstein
  • Status changed from new to assigned

Thanks for ticket and the ping, I will take a look!

@adamsilverstein commented on PR #9315:


4 months ago
#21

Looks good to me - thanks for the PR @heybran and the review @mukeshpanchal27!

#22 @adamsilverstein
4 months ago

PR looks good, thanks everyone for the fix here. I want to look a bit further to understand why this only breaks for SVGs but will plan get this committed for 6.8.3.

#23 @adamsilverstein
4 months ago

  • Keywords commit added

#24 @threadi
3 months ago

#63779 was marked as a duplicate.

This ticket was mentioned in Slack in #core-test by oglekler. View the logs.


3 months ago

#26 @iamshashank
3 months ago

  • Keywords needs-testing removed

#27 @SirLouen
3 months ago

  • Keywords needs-testing added

This ticket was mentioned in Slack in #core-test by rollybueno. View the logs.


3 months ago

This ticket was mentioned in Slack in #core-test by sirlouen. View the logs.


3 months ago

#30 @iamshashank
3 months ago

  • Keywords needs-test-info added

Reproduction Report

Description

This report validates whether the issue can be reproduced.

Environment

  • WordPress: 6.8.1-src
  • PHP: 8.2.28
  • Server: nginx/1.27.5
  • Database: mysqli (Server: 8.4.5 / Client: mysqlnd 8.2.28)
  • Browser: Chrome 138.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.2
  • MU Plugins:
    • allow-svg.php
  • Plugins:
    • Test Reports 1.2.0

Actual Results

  1. Unable to reproduce the issue.

Reproduction Steps

  1. Used this code to allow SVG in a mu-plugin
    <?php
    function wp_mime_types($mimes) {
            $mimes['svg'] = 'image/svg+xml';
            return $mimes;
    }
    add_filter('upload_mimes', 'wp_mime_types');
    
  1. Then I uploaded the SVG file to the media library
  1. In the theme’s functions.php, I added this code with the correct attachment I'd
    <?php
    add_action('wp_head', function() {
            echo wp_get_attachment_image(15, 'full', '', array('class' => 'mb-4', 'height' => 60, 'width' => 60));
     });
    
  1. Then i checked for both WP 6.8.1 and WP 6.8.2, but I cannot find the height and width attributes in any. I got everything same in both versions.

https://i.ibb.co/Hp1QDtSB/Screenshot-2025-08-11-at-10-28-20-PM.png
https://ibb.co/Hp1QDtSB

Additional Notes

  • This is what I did, if I'm doing something wrong, can you review my steps and tell me what I'm missing?"
Last edited 3 months ago by iamshashank (previous) (diff)

This ticket was mentioned in Slack in #core-test by iamshashank. View the logs.


3 months ago

#32 @iamshashank
3 months ago

  • Keywords needs-test-info removed

I figured out what was wrong while reproducing it, I had been inspecting the SVG after adding it to a page via the media library, and everything looked fine there. But when I checked the SVG on the homepage, that’s where the problem appeared. Now i'm able to reproduce the issue.

#33 @iamshashank
3 months ago

Test Report

Description

✅ This report validates that the indicated patch works as expected.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/9315.diff

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.28
  • Server: nginx/1.27.5
  • Database: mysqli (Server: 8.4.5 / Client: mysqlnd 8.2.28)
  • Browser: Chrome 138.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.3
  • MU Plugins:
    • allow-svg.php
  • Plugins:
    • Test Reports 1.2.0

Actual Results

  1. ✅ Issue resolved with patch.

Additional Notes

  • I was able to test this patch on WP version 6.9-alpha-60093-src, but when I tried the same patch on version 6.8.2, I got:
    1 out of 1 hunks failed--saving rejects to 'src/wp-includes/media.php.rej'
    

Screenshots

  • Before the patch - https://ibb.co/99Rg3K96
  • After the patch - https://ibb.co/VYwG7rdk

#34 @SirLouen
3 months ago

  • Keywords needs-testing removed

#35 @adamsilverstein
3 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 60641:

Media: ensure wp_get_attachment_image uses valid user-provided width and height.

Fix a bug introduced in WordPress 6.8.2 (r60415) that led to user supplied values for width and height in the $attr array passed to wp_get_attachment_image to be overwritten.

Props rainbowgeek, ocean90, rollybueno, shreya0shrivastava, heybran, mukesh27.
Fixes #63714.

#36 @adamsilverstein
3 months ago

  • Severity changed from major to normal

This is fixed in trunk (6.9). Reopening for consideration of back porting to 6.8.3.

#37 @adamsilverstein
3 months ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

#38 @jorbin
6 weeks ago

  • Milestone changed from 6.8.3 to 6.8.4

6.8.3 was a security only release. Punting though 6.8.4 is not planned as a release

#39 @adamsilverstein
6 days ago

Thanks everyone for your contributions on this ticket. Just noting that the fix is in trunk, meaning it will become available to everyone in a few weeks with the release of WordPress 6.9.

I will leave this open in case we want to ship the fix for the 6.8 branch as well.

#40 @desrosj
10 hours ago

  • Keywords commit removed
  • Milestone changed from 6.8.4 to 6.9
  • Resolution set to fixed
  • Status changed from reopened to closed

With 6.9 RC1 out, there are no plans to ship a 6.8.4 release. Moving this to the 6.9 milestone as that will be the first release containing the changes from this ticket.

This ticket was mentioned in Slack in #core by desrosj. View the logs.


10 hours ago

Note: See TracTickets for help on using tickets.