Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Source [email protected]
- // Website: http://www.digitalraindrops.net
- // I am working on a WordPress templating plugin and needed to upload new files to a specific folder in the uploads directory, without changing the default settings, I borrowed some code from wp-download
- // This code may help others, first this function re-directs the uploads to '/wp-content/uploads/mydirectory/images/'
- // First Example: Changed the Destination Directory for Uploads
- $sub_dir='/my-directory/images';
- function drfb_upload_dir($upload) {
- global $sub_dir;
- $upload['path'] = $upload['basedir'] .$sub_dir;
- $upload['url'] = $upload['baseurl'] .$sub_dir;
- return $upload;
- }
- // You call it before you upload, this sets it directory, you need to refresh or remove the filter after upload.
- add_filter('upload_dir', 'drfb_upload_dir');
- // Do the upload and the image will be uploaded to '/wp-content/uploads/mydirectory/images/'
- if( has_filter( 'upload_dir', 'drfb_upload_dir' ) )remove_filter( 'upload_dir', 'drfb_upload_dir' );
- // Second Example: this will take a returned libary image url, and copy it to the new directory.
- $dst= $upload['basedir'] .'/my-directory/images/';
- //Lets say the looked up image url is stored in an option.
- $myimage = get_option('my-image');
- if ( $myimage ) {
- $file_name = basename( $myimage );
- $file = WP_CONTENT_DIR .$file_name;
- if ( file_exists( $file ) && !file_exists( $dst .$file_name )) {
- if ( @copy( $file , $dst .$file_name ) ) update_option('my-image','');
- }
- }
Add Comment
Please, Sign In to add comment