Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 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.

share|improve this answer
    
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 at 23:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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