Back to Top

How to change File Upload Path in WordPress?

To change File/Image Upload Path in Wordpress

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

/* 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;
}

add_filter('upload_dir', 'my_image_upload_dir');

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

What’s New in Docker 1.12

Posted on 2 years ago

Bhumi

How to use Interface in PHP

Posted on 7 years ago

Bhumi

PHP Error Reporting Levels

Posted on 7 years ago

Bhumi

Introduction to PDO in PHP

Posted on 3 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 3 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 5 years ago

Bhumi