mcguffin / acf-dropzone

Drop file uploads directly on ACF file fields
https://wordpress.org/plugins/acf-dropzone/
GNU General Public License v3.0
32 stars 1 forks source link

JS Hook for Dropzone Args? Passed Post ID? #20

Closed HowdyMcGee closed 2 years ago

HowdyMcGee commented 2 years ago

Hello & thank you for putting together this plugin!

I'm trying to manage uploads by assigning them specific upload folders based on their post type. I'm doing so using the upload_dir filter hook and using the $_REQUEST array. To complicate this further, I'm using the ACF function acf_form() to do this on the frontend. Generally, WordPress will pass in a post_id in the $_REQUEST as well as the action, filename, etc. I was wondering if:

1) There was a JS action hook ( like acf has? ) so I could manually add in the Post ID to pass to the request. I see that you're already passing in _acfuploader as the field key as as param which is cool so I would need to extend that.

2) A mechanism built into the plugin that would also pass the Post ID along with the _acfuploader param to DropJS.

If I'm overlooking something on the WP side or in the ACF Drozone plugin that could help please let me know! Thanks again and I hope this finds you well!

HowdyMcGee commented 2 years ago

After looking into this a bit, one possible way that the plugin could include this is by adding the following to the wp.media.view.UploaderWindow instantiated object params in the acf-dropzone.js file:

params: {
    _acfuploader: this.field.get("key"),
    _post_id: this.closest( "form" ).getElementById( "_acf_post_id" )
},

Regardless of whether it's frontend or admin, Advanced Custom Fields adds the _acf_post_id hidden input under their acf-form-data section. Then, this could be checked and used in the upload_dir filter hook to change the uploaded file paths. To verify that this seems to work, I've error logged the $_REQUEST as seen below:

add_filter( 'upload_dir', function( $uploads ) {
    error_log( print_r( $_REQUEST, true ) );
    return $uploads;
} );
mcguffin commented 2 years ago

Hi Alex, I made some slight changes in the update, so that the value of _acf_post_id is now present in the $_REQUEST array. If _acf_post_id is strictly numeric $_REQUEST['post_id'] will be there too.