• I’m writing a plugin that has a admin page to allow uploading on images for use by the plugin.

    right now it uploads to the uploads folder. I would like to be able to upload them to a custom folder for example /uploads/myplugin.

    i use the built in media api by using the wp_enqueue_media() function. Can anyone provide me with sample code or point me in the right direction.

    Thanks

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I would use wp_handle_upload() to upload image files, using the “upload_dir” filter to alter where the file ends up. The trick is the filter callback must only be added for these particular uploads so other uploads remain unaffected. How that is done depends on the specifics of how you handle uploads in general.

    Another option would be to let it go to uploads initially, then move it (using rename() ) during a later point in the process. Again, that point depends on the specifics of how you handle uploads in general.

    I don’t know much about wp_enqueue_media(). I imagine uploads through that interface end up eventually using wp_handle_upload() anyway, so the challenge comes down to when to selectively add a “upload_dir” filter callback or when to move the file out of uploads. The media API probably uses Ajax to manage uploads. Use your browser’s developer tools to determine what “action” parameter is used in the Ajax request. This is translated into a handler function by prefixing “wp_ajax_” to the value. Find the source code for this function. You’ll likely find hooks that can be used to selectively add a “upload_dir” filter callback or move the file after upload.

Viewing 1 replies (of 1 total)

The topic ‘media upload to custom folder in custom plugin.’ is closed to new replies.