Make WordPress Core

Opened 11 years ago

Closed 9 years ago

#20720 closed defect (bug) (fixed)

attachment_fields_to_save filter is called twice after initial image upload in post

Reported by: williamsba1's profile williamsba1 Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.0 Priority: normal
Severity: normal Version: 3.3
Component: Media Keywords: has-patch
Focuses: Cc:


I believe I found a bug in WordPress with the attachment_fields_to_save filter. For some reason attachment_fields_to_save is called twice per image, but only if you "Save all changes" after uploading a new image to a post.

How to reproduce:

  1. Add the below code to a plugin:
add_filter( 'attachment_fields_to_save', 'test_attachment_fields_to_save', 10, 2 );

function test_attachment_fields_to_save( $post, $attachment ) {

	echo 'Hi! </br >';

	return $post;
  1. Create a new post
  2. Upload a single image and click "Save all changes"

You'll see "Hi!" displayed twice. Now visit the Gallery tab and save the same image and you'll see "Hi!" is only displayed once. This only happens when you save a new image uploaded to a post. If you save an image under the Gallery tab, or via the Media Library, the filter is only called once as expected.

I confirmed this is happening in 3.3.1 and trunk

Attachments (1)

20720.patch (950 bytes) - added by SergeyBiryukov 11 years ago.

Download all attachments as: .zip

Change History (5)

#1 @nacin
11 years ago

  • Version changed from 3.4 to 3.3

#2 @SergeyBiryukov
11 years ago

  • Keywords has-patch added

Both calls are in wp_media_upload_handler().

First, media_upload_form_handler() is called in line 555:

Then media_upload_gallery() is called, which has exactly the same block:

20720.patch is an attempt to correct the logic.

#3 @wonderboymusic
9 years ago

  • Milestone changed from Awaiting Review to 4.0

#4 @wonderboymusic
9 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 28871:

In deprecated media iframe code, attachment_fields_to_save filter should not be called twice after initial image upload in post.

Props SergeyBiryukov.
Fixes #20720.

Note: See TracTickets for help on using tickets.