i have been trying to upload the attachments to the custom directory without affecting the old uploads. so i use this bellow code to achieve that
add_filter('wp_handle_upload_prefilter', 'cust_pre_upload');
add_filter('wp_handle_upload', 'cust_post_upload');
function cust_pre_upload($file){
add_filter('upload_dir', 'cust_dir');
return $file;
}
function cust_post_upload($fileinfo){
remove_filter('upload_dir', 'cust_dir');
return $fileinfo;
}
function cust_dir( $args ) {
$customdir ="/mars";
$args['path'] = "C:/wamp/www/web1/wp-content";
$args['url'] = "http://localhost/web1/wp-content";
$args['subdir'] = $customdir;
$args['path'] .= $customdir;
$args['url'] .= $customdir;
return $args;
}
i use the wp_handle_upload_prefilter to assign the new directory only for the attachment i upload now or in future and wp_handle_upload to prevent the old attacmments. this code perfectly uploading to the folder where it supposed to [http://localhost/web1/wp-content/mars/file.jpg but the attachment get this wrong and show an invalid url of the attachment something like this [http://localhost/web1/wp-content/uploads/C:/wamp/www/web1/wp-content/mars/file.jpg] any help or idea would be appreciated :) tried different types of codes nothing worked out.
/
everywhere as you should?/mars
,C:/wamp/www/web1/wp-content
. Don’t worry about Windows, it will handle that without problems.