Make WordPress Core

Opened 6 years ago

Last modified 6 weeks ago

#48477 new enhancement

Add a hook before creating image sub-sizes

Reported by: greglone's profile GregLone Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 5.3
Component: Upload Keywords: has-patch 2nd-opinion needs-dev-note
Focuses: Cc:

Description

Since things are moving for media thumbnails lately, it could be a good time to add a hook that fires right before thumbnails are created.
It would allow plugins to perform operations on the main file for example.

Attachments (1)

48477.diff (753 bytes) - added by GregLone 6 years ago.

Download all attachments as: .zip

Change History (17)

@GregLone
6 years ago

#1 @SergeyBiryukov
6 years ago

  • Milestone changed from Awaiting Review to 5.3

Moving to the milestone for visibility.

#2 @jorbin
6 years ago

  • Milestone changed from 5.3 to 5.3.1

I chatted with @mikeschroder and we discussed that we want to spend some time ensuring that we get the parameters and placement correct for this, so bumping to the .1 release to allow time for the exploration.

#3 @GregLone
6 years ago

I understand, I'm late and we're close to the release. Thanks for considering it :)

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


6 years ago

#5 @marybaum
6 years ago

Ooh I like this!

#6 follow-up: @azaozz
6 years ago

  • Keywords reporter-feedback added

It would allow plugins to perform operations on the main file for example.

What would be the intended use there? Currently there are (at least) three hooks fired right after a file was uploaded:

These give full access to all aspects of handling an uploaded file. Do we really need a forth hook specific for images? :)

#7 in reply to: ↑ 6 ; follow-up: @GregLone
6 years ago

Replying to azaozz:

Currently there are (at least) three hooks fired right after a file was uploaded:

The idea is not to have a hook that triggers after a file is uploaded but, as the hook’s name suggests, before sub-sizes are created. The difference is that it can happen even if the file wasn’t just uploaded: when a plugin regenerates thumbnails for example.
Currently I’m also thinking adding one after the sub-sizes are created and recorded in the database, maybe at the end of wp_update_attachment_metadata(), with an additional context argument (create/update), like you suggested recently if I remember well.

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


6 years ago

This ticket was mentioned in Slack in #core-media by audrasjb. View the logs.


6 years ago

#10 in reply to: ↑ 7 @azaozz
6 years ago

  • Keywords reporter-feedback removed
  • Milestone changed from 5.3.1 to 5.4

Replying to GregLone:

Currently I’m also thinking adding one after the sub-sizes are created and recorded in the database, maybe at the end of wp_update_attachment_metadata(), with an additional context argument (create/update), like you suggested recently if I remember well.

Yeah, this can potentially be useful. At the same time I'd prefer not to "rush it" in 5.3.1. Perhaps better to confirm the use cases before adding the hooks. Then a good decisions can be made whether to pass context, etc.

Moving this to 5.4 perhaps pending a revised/updated patch.

#11 @GregLone
6 years ago

I agree, it will give us time to think a bit more about it.

#12 @davidbaumwald
6 years ago

  • Keywords needs-dev-note added

This will at least need a small call out in the misc Dev Notes once it lands in a cycle. Tagging as such.

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


6 years ago

#14 @davidbaumwald
6 years ago

  • Keywords needs-refresh added
  • Milestone changed from 5.4 to Future Release

This ticket still needs a final decision and a refreshed patch, and with 5.4 Beta 1 landing tomorrow, this is being moved to Future Release. If any maintainer or committer feels this can be included in 5.4 or wishes to assume ownership during a specific cycle, feel free to update the milestone accordingly.

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


7 months ago
#15

  • Keywords needs-refresh removed

Trac ticket: #48477

#16 @ozgursar
6 weeks ago

  • Keywords needs-testing removed

Patch Testing Report

Patch Tested: https://github.com/WordPress/wordpress-develop/pull/9453

Environment

  • WordPress: 7.0-alpha-61215-src
  • PHP: 8.2.29
  • Server: nginx/1.29.4
  • Database: mysqli (Server: 8.4.7 / Client: mysqlnd 8.2.29)
  • Browser: Chrome 144.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-One 2.7
  • MU Plugins: None activated
  • Plugins:
    • Code Snippets 3.9.4
    • Test Reports 1.2.1

Steps taken

  1. Add the following snippet to functions.php or via Code Snippets plugin
add_action('wp_before_make_subsizes', function($new_sizes, $file, $image_meta, $attachment_id) {
    error_log("=== wp_before_make_subsizes HOOK FIRED ===");
    error_log("Attachment ID: " . $attachment_id);
    error_log("File: " . $file);
    error_log("Sizes to create: " . implode(', ', array_keys($new_sizes)));
    error_log("==========================================");
}, 10, 4);
  1. Add an image to Media
  2. Ensure WP_DEBUG and WP_DEBUG_LOG are enabled in wp-config.php
  3. Check /wp-content/debug.log to confirm the hook is running as expected
  4. When I uploaded a random image, the following lines are logged:
[01-Feb-2026 12:30:30 UTC] === wp_before_make_subsizes HOOK FIRED ===
[01-Feb-2026 12:30:30 UTC] Attachment ID: 98
[01-Feb-2026 12:30:30 UTC] File: /var/www/src/wp-content/uploads/2026/02/images.jpg
[01-Feb-2026 12:30:30 UTC] Sizes to create: medium, large, thumbnail, medium_large, 1536x1536, 2048x2048, post-thumbnail
[01-Feb-2026 12:30:30 UTC] ==========================================

  1. ✅ Patch is solving the problem

Expected result

  • We are expecting wp_before_make_subsizes hook to run before creating image sub-sizes when an image is uploaded.
Note: See TracTickets for help on using tickets.