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?



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.

  • 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
    Oct 19 '14 at 23:22

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .