PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Can't hook wp_insert_attachment

  1. Hello,

    I am trying to hook the wp_insert_attachment function of wordpress,
    because I would like to preprocess the file upload from gravityforms.

    in the gravityforms function media_handle_upload I noticed the call to
    wp_insert_attachment like this:

    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if ( !is_wp_error($id) ) {
        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
    }

    I did the following in my plugin:

    public function __construct() {
        add_filter( 'wp_insert_attachment', array( $this, 'filter_insert' ), 1, 3 );
    }

    and the filter_insert function looks like this:

    public function filter_insert( $attachment, $file, $id){
    exit;
    }

    The result is, that the filter_insert function is not called...

    best

    regards

    kubante

    Posted 10 years ago on Thursday October 6, 2011 | Permalink
  2. Hi Kubante,

    The wp_insert_attachment() function does not appear to contain any filter titled "wp_insert_attachment". I do see two actions that might of use to you; however, they are called after the attachment has already been inserted/updated. http://grab.by/b2ew

    Using the "add_attachment" hook you could call your plugins custom functionality, post process the image and then "update" the attachment by sending the updated attachment object back to the wp_insert_attachment function.

    Posted 10 years ago on Monday October 10, 2011 | Permalink

This topic has been resolved and has been closed to new replies.