1

The wp_insert_attachment_data filter is available inside wp_insert_attachment(). It allows attachment post data to be filtered before it is updated in or added to the database.

$data = apply_filters( 'wp_insert_attachment_data', $data, $object ); 

I have no need to change the post data but I need to perform an action at this exact point in the execution. Is it acceptable to hook my function to the filter, do some processing and then return $data unaltered? I do actually need to use $data, but don't need to change it. For example:

function my_function( $data, $object ) {

    // Do some stuff with $data here.

    // No changes made to $data. Just return it.
    return $data;
}
add_filter( 'wp_insert_attachment_data', 'my_function', 10, 2 );

Although I believe this solution will work for my purpose, I feel it's a little 'hackish' and is at odds with the purpose of the wp_insert_attachment_data filter. The purpose of a filter is to modify some variable. In my case, I'm not modifying anything.

Is this acceptable or should I ask on WordPress Trac for an action hook to be added?

1

All actions are actually filters internally.

So yes, you can do things in a filter hook. However, be mindful of which filters and what you're doing. Some filters are ran everywhere, and doing expensive operations on them may lead to massive slowdowns. Also beware of recursion. If your expensive operation triggers the very filter you've hooked on to..

  • Thanks for the info and thanks for the pointers on what to look out for. – henrywright Sep 14 '14 at 23:01
  • +1. I only would add that when hooking a filter, the hooking callback must always return the first argument reveived, modified or not. – gmazzap Sep 14 '14 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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