Make WordPress Core

Opened 10 years ago

Last modified 5 years ago

#28774 new enhancement

Hooking into wp_ajax_upload_attachment

Reported by: jshreve's profile jshreve Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.0
Component: General Keywords: has-patch dev-feedback
Focuses: Cc:

Description

If you want to do something before/after an attachment has been uploaded - or replace the upload function entirely -- there doesn't seem to be a way to do that:

For other wp_ajax_* calls, you can unhook them and then hook in your own. This doesn't work for wp_ajax_upload_attachment because it ends up getting called directly from async-upload.php.

If we replace the direct call with another set of do_action/add_action (like admin-ajax has) you can now hook into upload like the other ajax actions.

See the proposed patch.

Attachments (1)

async-upload-attachment.diff (492 bytes) - added by jshreve 10 years ago.

Download all attachments as: .zip

Change History (4)

#1 follow-up: @nacin
10 years ago

  • Keywords reporter-feedback added

I think unhooking core wp_ajax_* hooks is kind of at-your-risk. I'm not going to pretend that those are API, and I imagine we've already broken them a number of times at this point.

An action in wp_ajax_upload_attachment() could make sense, but really, there should already be hooks deeper in the process, such as in wp_handle_upload(). What are you trying to do?

#2 in reply to: ↑ 1 @jshreve
10 years ago

Replying to nacin:

What are you trying to do?

There are a few places where we are embedding stuff from core (like the media manager or wplink) on NewDash (WordPress.com / blog id 1) - for example in the new posting experience at WordPress.com/post/.

To embed/use the functionality outside of wp-admin we pass in a blog ID to the ajax call and unhook some of the wp_ajax_* hooks, rehook with switch_to_blog; cap check, call the wp_ajax_* function, restore_current_blog.

I don't think I ended up needing to do anything with wp_ajax_upload_attachment itself - but I think the patch is still valid (its the only wp_ajax_* action you can't touch with hooks).

Last edited 10 years ago by jshreve (previous) (diff)

#3 @chriscct7
8 years ago

  • Keywords has-patch dev-feedback added; reporter-feedback removed
Note: See TracTickets for help on using tickets.