1. Home
  2. Knowledge Base
  3. Custom Development
  4. Add a custom path for file upload

Add a custom path for file upload

Files uploaded into Download Monitor will be saved by default in the restricted folder: wp-content/uploads/dlm_uploads.

If you are looking to add a custom path for uploaded files please add the code below in your theme’s functions.php file or your child theme’s functions.php file.

<?php

add_filter( 'upload_dir', 'change_upload_dir_for_dlm', 30 );
function change_upload_dir_for_dlm( $pathdata ){
	
	if ( isset( $_POST['type'] ) && 'dlm_download' === $_POST['type'] ) { 
		if ( empty( $pathdata['subdir'] ) ) {
			$pathdata['path']   = $pathdata['path'] . '/private';
			$pathdata['url']    = $pathdata['url'] . '/private';
			$pathdata['subdir'] = '/private';
		} else {
			$new_subdir = '/private' . $pathdata['subdir'];

			$pathdata['path']   = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] );
			$pathdata['url']    = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] );
			$pathdata['subdir'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['subdir'] );
		}
	}

	return $pathdata;
	
}

This is useful for servers that have their own restricted folder. For example, Pantheon uses wp-content/uploads/private folder for private files (avoid direct access to the files).

Was this article helpful?

Related Articles

Start Protecting your WordPress Downloads

Protect, sell and track downloads on Wordpress... What are you waiting for?