1

I have a plugin that hooks into "wp_handle_upload" filter. Within that scope I'd like to be able to identify where the upload came from, meaning I want to treat it differently if it was uploaded from within a post vs. the media library vs. a plugin/theme settings page (for example the header image for a template).

The only thing passed into my function is an array with file,url,type. Is there a way to identify where it came from?

I could probably create an exception and examine the stack trace, is that a decent solution? If so, what might I be looking for in the stack?

Thanks!

1 Answer 1

1

I was able to figure it out by looking at the form parameters, which I don't really like doing since it's not part of the wordpress api. if anybody has a better idea, please leave a comment.

$_REQUEST['post_id'] > 0 // it's being uploaded from a page/post
$_REQUEST['post_id'] == 0 // it's being uploaded from the media library
empty($_REQUEST['post_id']) // it came from somewhere else (plugin, theme, etc)

It seems to be reliable for posts and library, but the "other" is probably not 100% reliable since plugins and themes could add parameters to trick the code into thinking it was one of the other types.

1
  • Thanks for this Jason, I'm really interested in doing the same but when I log out $_REQUEST I'm getting empty in my logs. Do you know if this is still working with the latest version (4.0 at the time of this writing) of WordPress?
    – Brian
    Commented Oct 19, 2014 at 23:22

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.