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?



3 Answers 3


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:

add_action('admin_init', 'redirect_after_media_save');
function redirect_after_media_save() {

    global $pagenow;

    // when visiting the upload screen, we save the number of attachments
    if ( $pagenow == 'media-new.php' ) {
        $attachments_before = array_sum((array)wp_count_attachments());
        update_option('count_attach_before', $attachments_before);

    if ( $pagenow == 'upload.php' ) { // we are on media library page

        // get attachments count before and after upload
        $attachments_before = get_option('count_attach_before');
        $attachments_after = array_sum((array)wp_count_attachments());

        if ( 
            // there are new files uploaded
            ( wp_get_referer() == admin_url('media-new.php') && $attachments_after > $attachments_before )
            // or we have just edited media file
        ) {
                // redirect to desired location

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.

function cpt_from_attachment($attachment_ID)
    global $current_user;

    $attachment_post = get_post( $attachment_ID );

    $type = get_post_mime_type($attachment_ID);
    if(strpos($type, 'audio') === 0)
        // Create new custom post object only for audio files
        $my_post = array(
          'post_title'    => $attachment_post->post_title,
          'post_content'  => $attachment_post->post_content,
          'post_type'   => 'talk',
          'post_author'   => $current_user->ID

        // Insert the custom post into the database
        $post_id = wp_insert_post( $my_post );
        wp_update_post( array(
                'ID' => $attachment_ID ,
                'post_parent' => $post_id
        wp_set_post_terms( $post_id, get_post_meta($attachment_ID, "artist", true), 'speaker' );

add_action("add_attachment", 'cpt_from_attachment');

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.

add_filter('attachment_fields_to_save', 'attachment_stuff');
function attachment_stuff($stuff){

    //Do stuff here, but don't echo anything or you'll break the filter.

    return $stuff;

$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'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.