How to Change Upload Folder URL for Every Uploads in WordPress |

Congrats, You are Subscribed to Receive Updates.

How to Change Upload Folder URL for Every Uploads in WordPress

You are here to know about changing uploads url dynamically for your every uploads. Before getting into the topic, just look at here for front end multiple file upload, once you know the things, than it will easier for you to change uploads path for every uploads, Click here to read the basic information .

Ok, Now you got some basic idea regarding multiple file uploads, Than here is our easy way to change uploads folder for every file upload.  And than we are going to write a function for new uploads path as like the following one.

function kv_custom_post_dir( $pathdata ) {
	$subdir = '/post_img'.$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;

The Above function will change your uploads path, But we need to use it on our right time to change the uploads url.  From my previous post, i created multiple file uploads, there open kv-uploads.php file and add  the following line .

add_filter('upload_dir', 'kv_custom_post_dir');

just add this line before you call the “kv_handle_attachment” function.  Also add the following line of code after the “kv_handle_attachment” Function.

remove_filter('upload_dir', 'kv_custom_post_dir');

That’s it, here is the complete code.

   if ( $_FILES ) { 
	$files = $_FILES["kv_multiple_attachments"];  
	foreach ($files['name'] as $key => $value) { 			
		if ($files['name'][$key]) { 
			$file = array( 
				'name' => $files['name'][$key],
	 			'type' => $files['type'][$key], 
				'tmp_name' => $files['tmp_name'][$key], 
				'error' => $files['error'][$key],
 				'size' => $files['size'][$key]
			$_FILES = array ("kv_multiple_attachments" => $file); 
			foreach ($_FILES as $file => $array) {				              
                                add_filter('upload_dir', 'kv_custom_post_dir');
				$newupload = kv_handle_attachment($file,$pid); 
                                remove_filter('upload_dir', 'kv_custom_post_dir');

Remember , kv_custom_post_dir();  function handles the folder path. So you can customize any folder name inside it. There you can get current post id and use it for folder name.


About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level


  1. commenter
    Purushothaman .K

    Nice Article. And Like to know… Is there any way to store uploaded file url in custom Database. Any suggestion, help would do a favour. Hoping more tutorial From your site.

    • commenter

      Yes you can save the uploads attachments on a custom database. But you need to create another object by creating a object to wpdb class.

      But its little more complex to play with another database.

  2. commenter

    Hi Varadharaj,

    Thanks for this tutorial. I am wondering though how to create multiple directories for image files uploaded through the backend of a WordPress site (so I don’t need the frontend loading function). I don’t know much about writing PHP. Hope you can advise.


    • commenter

      Thanks for your comment. You can change uploads folder for every uploads, there is a simple thing behind it to get separate uploads folder for every uploads,

      add_filter('upload_dir', 'kv_custom_post_dir');

      use a if statement to to check whether the attached file is image or doc, than if its image means, use a separate add filter with folder name. You have to write all you codes inside “kv_custom_post_dir” to take effect.

Comment Below

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


Current ye@r *