WordPress action & filter hook · part of WordPress core

wp_generate_attachment_metadata

A WordPress core action & filter that 21 plugins also fire themselves, running on 569K+ sites between them.

“This filter is documented in wp-admin/includes/image.php”

How to use wp_generate_attachment_metadata

It's a filter: your callback receives the value and 1 more argument, and must return the (changed) value. Add this to your theme's functions.php or a small custom plugin:

functions.php
add_filter( 'wp_generate_attachment_metadata', function( $metadata, $attachment_id ) {
	// Change $metadata here.
	return $metadata;
}, 10, 2 );

Argument names come from Gallery by BestWebSoft's source. Other plugins may pass different values: check the call in the plugin you're targeting below.

This hook is fired by WordPress core too. Official reference on developer.wordpress.org →

21 plugins that fire wp_generate_attachment_metadata

Sorted by active installs. Each entry shows where the plugin calls the hook and what it passes.

  1. Gutenberg icon Gutenberg400,000+ installs apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'update' ) lib/media/class-gutenberg-rest-attachments-controller.php:1687
  2. Simple Image Sizes icon Simple Image Sizes60,000+ installs apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'create' ) classes/Admin/Main.php:331
  3. Image Watermark icon Image Watermark40,000+ installs apply_filters( 'wp_generate_attachment_metadata', $plan['metadata'], $attachment_id, 'update' ) includes/class-upload-handler.php:16110
  4. AJAX Thumbnail Rebuild icon AJAX Thumbnail Rebuild30,000+ installs apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'update' ) includes/class-atr-regenerator.php:156
  5. Nexter Extension icon Nexter Extension10,000+ installs apply_filters( 'wp_generate_attachment_metadata', $thumbnail_metadata, $thumbnail_id, 'update' ) include/panel-settings/extensions/nexter-ext-image-sizes.php:363
  6. Gallery by BestWebSoft icon Gallery by BestWebSoft10,000+ installs apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ) gallery-plugin.php:3643
  7. Classified Listing icon Classified Listing8,000+ installs apply_filters( 'wp_generate_attachment_metadata', $image_meta, $attachment_id, 'create' ) app/Traits/Functions/MediaTrait.php:385
  8. Plus WebP or AVIF icon Plus WebP or AVIF5,000+ installs do_action( 'wp_generate_attachment_metadata', $metadata_org, $attach_id ) lib/class-pluswebpadmin.php:259
  9. Cowboy MCP icon Cowboy MCP2,000+ installs apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'create' ) includes/class-mcp-compat.php:584
  10. MStore API icon MStore API2,000+ installs apply_filters( 'wp_generate_attachment_metadata', $attachment, $attachment_id, 'create' ) functions/index.php:1628
  11. WPCOM Member icon WPCOM Member1,000+ installs apply_filters( 'wp_generate_attachment_metadata', $mirror, 0, 'create' ) includes/class-member.php:1352
  12. Portfolio by BestWebSoft icon Portfolio by BestWebSoft600+ installs apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ) portfolio.php:1589
  13. Thumbnail Manager icon Thumbnail Manager200+ installs apply_filters( 'wp_generate_attachment_metadata', $proposed, $snapshot['attachment_id'], 'update' ) inc/media/regeneration.php:397
  14. GrandPlugins Image Watermark icon GrandPlugins Image Watermark200+ installs apply_filters( 'wp_generate_attachment_metadata', $image_metadata, $attachment_id, 'update' ) includes/class-single-apply-watermarks.php:304
  15. Large Images Uploader icon Large Images Uploader100+ installs apply_filters( 'wp_generate_attachment_metadata', $image_meta, self::$current_attachment_id, 'update' ) includes/WP_Large_Images_Uploader.php:286
  16. Ad King Pro icon Ad King Pro100+ installs apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment->ID ) includes/admin_area.php:343
  17. Generate Thumbnail icon Generate Thumbnail20+ installs apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ) admin/includes/getmbl-admin-functions.php:97
  18. Blaminhor Essentials icon Blaminhor Essentials20+ installs apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'create' ) modules/image-sizes/class-module-image-sizes.php:695
  19. Cloage WordPress Plugin icon Cloage WordPress PluginFewer than 10 installs apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ) includes/Media/Attachment/Attachment.php:348
  20. Linko Media Path Mapper & Swapper icon Linko Media Path Mapper & SwapperFewer than 10 installs do_action( 'wp_generate_attachment_metadata', $attach_data, $current_id ) includes/class-ajax-handler.php:565
  21. Kodlo Media Replace icon Kodlo Media ReplaceFewer than 10 installs apply_filters( 'wp_generate_attachment_metadata', $meta, $id, 'create' ) kodlo-media-replace.php:522
  22. CodePros Image Optimizer icon CodePros Image OptimizerFewer than 10 installs apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'cpiop_webp_conversion' ) includes/Ajax/AjaxHelper.php:44
  23. Npcink Site Toolbox icon Npcink Site ToolboxFewer than 10 installs apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'update' ) admin/partials/performance/media_health/webp_batch.php:185

Need a customisation built on this hook?

Tell us what you want to change and which plugin it's for. We'll write it, test it and send it back as a small plugin.