Well,This is a continuation from my last post on File/Image Upload in WordPress. I realized after I had written that tutorial that I would need to do a one more tutorial on how to change file/image upload path in WordPress before just uploading function.
We always need changes in WordPress default functionality to match with our requirement.In this tutorial, you will learn how to change the path of the WordPress file to upload. I will show you by the example of function and how to do one yourself.
Read about: Cookie Free domain in WordPress
1 2 3 4 5 6 7 8 9 10 11 12 | /* To Change path of file to uplod */ function my_image_upload_dir( $pathdata ) { $subdir = '/my_images/'.$pathdata['subdir']; $pathdata['path'] = str_replace($pathdata['subdir'], $subdir, $pathdata['path']); $pathdata['url'] = str_replace($pathdata['subdir'], $subdir, $pathdata['url']); $pathdata['subdir'] = str_replace($pathdata['subdir'], $subdir, $pathdata['subdir']); return $pathdata; } |
You can add above filter before you use the code for adding attachments
and below line of code to remove changes so it does not affect all attached files.
1 2 3 | remove_filter('upload_dir', 'my_image_upload_dir'); |
And there you have it. You’ve just successfully changed the path of the file to upload into your WordPress theme in a user-friendly way.
Suggested Read: Get all images from single post in WordPress
I’m hoping that you’ve found this article helpful. Don’t hesitate to use this code in your WordPress projects and post your comments if you need help. As always, thanks for reading. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.