Skip to content

Media: Fix WP_Site_Icon 'width ' typo causing non-square site icon subsizes - #11975

Open
NoumaanAhamed wants to merge 5 commits into
WordPress:trunkfrom
NoumaanAhamed:fix/65345-fix-incorrect-site-icon-dimension
Open

Media: Fix WP_Site_Icon 'width ' typo causing non-square site icon subsizes#11975
NoumaanAhamed wants to merge 5 commits into
WordPress:trunkfrom
NoumaanAhamed:fix/65345-fix-incorrect-site-icon-dimension

Conversation

@NoumaanAhamed

Copy link
Copy Markdown

Summary

Fixes an issue where site icon subsizes (site_icon-192, site_icon-32, etc.) are generated with incorrect, non-square dimensions instead of the expected square crops (e.g. 192×192, 32×32).

Root Cause

In src/wp-admin/includes/class-wp-site-icon.php, the WP_Site_Icon::additional_sizes() method defines subsize entries with the array key 'width ' (containing a trailing space) instead of 'width':

$only_crop_sizes[ 'site_icon-' . $size ] = array(
    'width ' => $size,  // ← trailing space in key
    'height' => $size,
    'crop'   => true,
);

The downstream consumer in src/wp-includes/media.php (wp_get_registered_image_subsizes()) reads the width using the correct key 'width' (no space):

if ( isset( $additional_sizes[ $size_name ]['width'] ) ) {
    $size_data['width'] = (int) $additional_sizes[ $size_name ]['width'];
}

Because the keys don't match, the isset() check fails, width remains 0, and the image editor never receives a width constraint. This results in non-square, aspect-ratio-preserved thumbnails instead of the intended square crops.

This typo has existed since the WP_Site_Icon class was introduced in WordPress 4.3.

Solution

  • Changed 'width ' to 'width' in the subsize definition array inside WP_Site_Icon::additional_sizes().
  • Updated the corresponding test expectations in tests/phpunit/tests/image/siteIcon.php which mirrored the same typo.

Testing

  1. Set up a local WordPress dev environment (e.g. using wp-env, Local, or Docker).
  2. Upload a non-square image (e.g. a 1200×800 landscape photo) via Media → Add New.
  3. Set it as the site icon without cropping:
    wp option update site_icon <attachment_id>
  4. Regenerate the attachment metadata with the site icon filters enabled (this is necessary because by default WP-CLI does not load admin/customizer classes containing site icon filters):
    wp eval '
    require_once ABSPATH . "wp-admin/includes/class-wp-site-icon.php";
    $wp_site_icon = new WP_Site_Icon();
    add_filter( "intermediate_image_sizes_advanced", array( $wp_site_icon, "additional_sizes" ) );
    $metadata = wp_generate_attachment_metadata( <attachment_id>, get_attached_file( <attachment_id> ) );
    wp_update_attachment_metadata( <attachment_id>, $metadata );
    '
  5. Inspect the generated subsizes:
    wp post meta get <attachment_id> _wp_attachment_metadata
  6. Verify that site_icon-270, site_icon-192, site_icon-180, and site_icon-32 are now square crops (e.g. 192×192, 32×32). Before the fix, they will be non-square dimensions matching the source image's aspect ratio.

Trac Ticket: https://core.trac.wordpress.org/ticket/65345


Test Case Expected / Result Status
test_additional_sizes — subsizes use 'width' key Array keys match 'width' (no trailing space) ✅ Passed
test_additional_sizes_with_filter — custom sizes use 'width' key Array keys match 'width' (no trailing space) ✅ Passed
Site icon subsizes are square crops after regeneration site_icon-192 = 192×192, site_icon-32 = 32×32, etc. ✅ Passed
get_site_icon_url() returns correct subsize URL Returns subsize URL instead of falling back to full image ✅ Passed

Use of AI Tools

AI assistance: No

@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mohamedahamed, peterwilsoncc, wildworks, yusufmudagal, juanmaguitar.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@yusufhay

yusufhay commented Jun 8, 2026

Copy link
Copy Markdown

Tested the patch locally against current origin/trunk in a disposable worktree.

Results:

  • php -l src/wp-admin/includes/class-wp-site-icon.php passed.
  • vendor/bin/phpunit --filter Tests_WP_Site_Icon passed: 7 tests, 11 assertions.

The change looks focused to the incorrect width key and the updated Site Icon expectations pass for me.

@juanmaguitar

juanmaguitar commented Jul 23, 2026

Copy link
Copy Markdown

Patch Testing Report

Environment

  • WordPress: 7.1-beta3-62828-src
  • PHP: 8.2.29
  • Server: nginx 1.29.4
  • Database: MySQL 8.4.8
  • OS: macOS (Docker local-env)
  • Theme: Twenty Twenty-Five 1.5
  • MU Plugins: none
  • Plugins: none

Steps taken

  1. Uploaded a non-square image (680x1024) and set it as the site icon via wp option update site_icon, skipping the Customizer crop.
  2. Regenerated the attachment metadata so WP_Site_Icon::additional_sizes() ran.
  3. Checked the generated sizes. Before the patch, site_icon-192 came out as 128x192 instead of square.
  4. Applied the patch and repeated the same steps with a fresh upload.
  5. ✅ Patch is solving the problem. site_icon-192 is now 192x192, and the other site_icon sizes are square too.
  6. Ran phpunit --filter Tests_WP_Site_Icon after updating the two test assertions the patch touches: 7 tests, 11 assertions, all green.

Expected result

Site icon subsizes (site_icon-32, -64, -180, -192, -270) should always be square crops, no matter the source image's aspect ratio.

Additional Notes

The old tests actually asserted the buggy 'width ' key, so they were passing before the fix without ever catching the bug. Worth knowing this typo's been in core since WP 4.3.

Screenshots/Screencast with results

  • Before: site_icon-192 -> 128x192
  • After: site_icon-192 -> 192x192

Support Content

Trac ticket: https://core.trac.wordpress.org/ticket/65345

@juanmaguitar

juanmaguitar commented Jul 23, 2026

Copy link
Copy Markdown

@NoumaanAhamed One suggestion: test_additional_sizes() just compares the output array to a hardcoded copy with the same typo, so it can never catch this kind of bug. It may be worth adding a test that actually generates attachment metadata for a non-square image and asserts the site_icon-* subsizes come out square, that tests real behavior instead of the array matching itself.

Copilot AI review requested due to automatic review settings July 28, 2026 06:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot wasn't able to review any files in this pull request.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot wasn't able to review any files in this pull request.

The existing `test_additional_sizes()` builds its expected value by mirroring
the implementation, so it passed both with and without the `'width '` typo and
could never catch this class of bug.

Add a test that generates attachment metadata for a non-square source with the
`intermediate_image_sizes_advanced` filter applied, and asserts every
`site_icon-*` sub-size comes out square. This exercises the actual image editor
path, where a mismatched width key silently degrades the crop into an aspect
ratio preserving resize.

Co-Authored-By: Claude <noreply@anthropic.com>
@t-hamano

t-hamano commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

It may be worth adding a test that actually generates attachment metadata for a non-square image and asserts the site_icon-* subsizes come out square, that tests real behavior instead of the array matching itself.

Addressed in ab3661f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants