How to change Wordpress programmatically download folder?

I have a custom theme in which I created a download button where I call the Wordpress Uploader when these buttons are clicked. Something like that:

jQuery('#ahng_blog_upload_button').click(function() { formfield = jQuery('#ahng_blog_upload_image').attr('name'); tb_show('Upload or Select Photo and Click on "Insert into Post"', 'media-upload.php?type=audio&TB_iframe=true'); return false; }); 

Right now, in my functions.php folder, I set where I want the files to download ( http://website.com/wp-content/uploads ), but I want to change the download location depending on which button is clicked .

For example, when the "Download Image" button is clicked, change the download folder to images ( http://website.com/wp-content/uploads/images ), and when you click the "Download MP3" button, change the upload folder to audio ( http://website.com/wp-content/uploads/audio ).

I know that I can upload everything to the default folder that I mentioned earlier, but itโ€™s easier to sort it later if I need to find a specific file using ftp.

Is there a way to do this with jquery and ajax and call a php file that actually returns nothing but makes changes to Upload Dir in wordpress? Or in any other way?

+4
source share
1 answer

The method used is based on pure WordPress methods not related to jQuery or Ajax.

And I really donโ€™t know which buttons you are referring to ... There was a time when the only button is โ€œDownload Mediaโ€. If you do not use individual settings.

This WordPress StackExchange answer has other filtering options. Bellow is code configured to filter downloads by media type.

 add_filter('wp_handle_upload_prefilter', 'wpse_25894_handle_upload_prefilter'); add_filter('wp_handle_upload', 'wpse_25894_handle_upload'); function wpse_25894_handle_upload_prefilter( $file ) { add_filter('upload_dir', 'wpse_25894_custom_upload_dir'); return $file; } function wpse_25894_handle_upload( $fileinfo ) { remove_filter('upload_dir', 'wpse_25894_custom_upload_dir'); return $fileinfo; } function wpse_25894_custom_upload_dir($path) { // Determines if uploading from inside a post/page/cpt // If not, default Upload folder is used $use_default_dir = ( isset($_REQUEST['post_id'] ) && $_REQUEST['post_id'] == 0 ) ? true : false; if( !empty( $path['error'] ) || $use_default_dir ) return $path; //error or uploading not from a post/page/cpt // Save uploads in FILETYPE based folders. When using this method, // you may want to change the check for $use_default_dir $extension = substr( strrchr( $_POST['name'], '.' ), 1 ); switch( $extension ) { case 'jpg': case 'png': case 'gif': $customdir = '/images'; break; case 'mp4': case 'm4v': $customdir = '/videos'; break; case 'txt': case 'doc': case 'pdf': $customdir = '/documents'; break; default: $customdir = '/others'; break; } //remove default subdir (year/month) $path['path'] = str_replace($path['subdir'], '', $path['path']); $path['url'] = str_replace($path['subdir'], '', $path['url']); $path['subdir'] = $customdir; $path['path'] .= $customdir; $path['url'] .= $customdir; return $path; } 
+2
source

Source: https://habr.com/ru/post/1401109/


All Articles