Howdi,
I am trying to fire some code that will occur when I save an image in the media library, as per usual, I select the file, upload, enter the meta data and then click save. At this point I want to hook into wordpress, and redirect wordpress to another URL. This is so that rather than taking the user to a list of uploaded media, I can then take them to an image manipulation tool instead.
I found a filter that fires just before the meta data is created, but nothing for when the file is saved?
Thanks,
Actually there are no hook fired after media file(s) upload, at least as far as I know. The problem with the hooks available along the way of uploading and saving media files and data is that they are limited to one portion of the process, and so using any of them is not reliable. For example,
add_attachment
is fired after a new file completed the upload process before editing the file metadata, and if you intend to redirect the user at this point, it will break the upload process for subsequent files if we have more than one file upload, it might be suitable though for other kinds of actions.For your specific case however, you can hook to
admin_init
action hook and check if we are on media library screen after we have uploaded or edited file, we know that by saving the number of attachments before upload and compare it agianst the number of attachments after the upload:This code will redirect the user to dashboard after successful upload or edit of media file, you can adjust it to suit your need. You may also want to choose admin hook other than
admin_init
if you wish to perform tasks other than redirection.Perhaps a bit late answer, but I had a similar scenario and wanted to share the solution.
In
functions.php
of the theme (creating a plugin would work as well), I used the'add_attachment'
hook to create a new post (custom post type ‘talk’) based on each new uploaded file. Of course, the example could do with a bit of shining up, but this worked for parsing each media attachment that was uploaded.Looks like there is no action on media save, but there is a filter. Unfortunately that means you can do some stuff, you just cant actually echo anything out or you’ll break the filter.
$stuff is actuall an array of fields being used by the media item page. You could try other things like hooking into a more generic admin panel hook, and checking for the $_GET[‘action’] value of ‘editattachment’.