Title: [Plugin: Custom Upload Dir] Path based on Taxonomy?
Last modified: August 19, 2016

---

# [Plugin: Custom Upload Dir] Path based on Taxonomy?

 *  Resolved [cmurockstar](https://wordpress.org/support/users/cmurockstar/)
 * (@cmurockstar)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/plugin-custom-upload-dir-path-based-on-taxonomy/)
 * I love this plug-in. Quick question – i see you have categories as a path item,
   can you also do taxonomies that you make yourself?
 * For example, if i have a movie taxonomy with Drama, Horror, Sci-Fi, etc. Can 
   I have my path be:
 * /%movie_taxnomy%/
 * Thanks
 * [http://wordpress.org/extend/plugins/custom-upload-dir/](http://wordpress.org/extend/plugins/custom-upload-dir/)

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Thread Starter [cmurockstar](https://wordpress.org/support/users/cmurockstar/)
 * (@cmurockstar)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/plugin-custom-upload-dir-path-based-on-taxonomy/#post-1830009)
 * Hey – so i hacked your code – feel free to use it. Basically I created a new 
   term you can add to get the first term from a custom taxonomy. There is the skeleton
   to make it do more, but you can at least get 1 term and make it the path.
 * add this after line 106
    `$customdir = (strpos($customdir, '%post_taxonomy_%')!
   == false)? cud_find_and_replace_taxonomies($post_id,$customdir): $customdir;`
 * and then after the function cud_get_categories_name, add
 *     ```
       #/--------------- HACK - START ----------------------------\
   
       function cud_find_and_replace_taxonomies($post_id,$customdir){
       	//$prefix = "%post_taxonomy_%";
       	//$postfix = "%_post_taxonomy%";
       	//str_replace($prefix.$postfix,"josh-test01", $customdir);
       	return cud_find_and_replace_taxonomies_helper($post_id,$customdir);
       }
   
       function cud_find_and_replace_taxonomies_helper($post_id,$customdir){
       	$prefix = "%post_taxonomy_%";
       	$postfix = "%_post_taxonomy%";
   
       	$prefix_length = strlen($prefix);
       	$postfix_length = strlen($postfix);
   
       	$prefix_index = strpos($customdir,$prefix);
       	$postfix_index = strpos($customdir,$postfix);
   
       	if(($prefix_index !== false) && ($postfix_index !== false)){
       		$str_left = substr($customdir,0,$prefix_index);
   
       		$postfix_ending_index = $postfix_index+$postfix_length;
       		$str_right_length = strlen($customdir)-$postfix_ending_index;
       		$str_right = substr($customdir,$postfix_ending_index,$str_right_length);
   
       		$prefix_ending_index = $prefix_index+$prefix_length;
   
       		$taxonomyName = substr($customdir,$prefix_index+$prefix_length,$postfix_index-$prefix_ending_index);
   
       		return cud_find_and_replace_taxonomies_helper($post_id,$str_left.cud_get_taxonomy($post_id,$taxonomyName).$str_right);
       	}
       	/*
       		clear all unused with placeholders
       	*/
       	else{
       		return str_replace($prefix,"-cud-pr-", str_replace($postfix,"-cud-po-", $customdir));
       	}
   
       }
   
       function cud_get_taxonomy($post_id,$taxonomyName){
       	return cud_get_taxonomies($post_id,$taxonomyName,1);
       }
   
       function cud_get_taxonomies($post_id,$taxonomyName,$count){
       	$terms_array = get_the_terms($post_id,$taxonomyName);
   
       	if(is_array($terms_array)){
       		$terms = array();
   
       		if($count == -1){
       			foreach($terms_array as $term_object){
       				$terms[] = $term_object->slug;
       			}
       		}else{
       			for ( $counter = 0; $counter < count($terms_array) && $counter < $count; $counter += 1) {
       				$terms[] = $terms_array[$counter]->slug;
       			}
       		}
       		return implode('-', $terms);
       	}else{
       		return "_".$taxonomyName;
       	}
       }
   
       #\--------------- HACK - END   ----------------------------/
       ```
   
 * Then in cud_option_page, add the following to $placeholder
    `'post_taxonomy_%%
   _post_taxonomy' => __('The post\'s first term in the specified taxonomy. Place
   taxonomy name between the two delimiters', 'cud'),`
 * Enjoy
    Please contact me if you want the php file.
 *  Plugin Author [ulfben](https://wordpress.org/support/users/ulfben/)
 * (@ulfben)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/plugin-custom-upload-dir-path-based-on-taxonomy/#post-1830015)
 * Beautiful, thank you very much for helping out!
 * I’d love to get some details on your use case for taxonomies. I’m not a heavy
   blog user myself and am not sure what taxanomies are or how they’re used in practice.
 * It seems your hack requires a new way of using the configuration panel – requiring
   a prefix and postfix.
 * I suggest we allow users to input ‘%taxanomy-name%’ instead (‘taxanomy-name’ 
   would obviously be whatever taxonomy you want to build a path from). If there’s
   no terms defined we throw that part (of the path) out.
 * Makes sense?
 *  Thread Starter [cmurockstar](https://wordpress.org/support/users/cmurockstar/)
 * (@cmurockstar)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/plugin-custom-upload-dir-path-based-on-taxonomy/#post-1830039)
 * Oh yes – your idea works much better – I just wanted something i could code up
   quickly; clearly your solution would be more elegant.
 * I can write you offline about my use case so not to clutter the forms
 *  Plugin Author [ulfben](https://wordpress.org/support/users/ulfben/)
 * (@ulfben)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/plugin-custom-upload-dir-path-based-on-taxonomy/#post-1830227)
 * [This is in version 3.3 btw](http://wordpress.org/extend/plugins/custom-upload-dir/).
   Sorry for not updating the thread. You can use %category%, %tags% or any of your
   own custom taxonomies straight up.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘[Plugin: Custom Upload Dir] Path based on Taxonomy?’ is closed to new
replies.

 * ![](https://s.w.org/plugins/geopattern-icon/custom-upload-dir.svg)
 * [Custom Upload Dir](https://wordpress.org/plugins/custom-upload-dir/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-upload-dir/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-upload-dir/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-upload-dir/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-upload-dir/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-upload-dir/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [ulfben](https://wordpress.org/support/users/ulfben/)
 * Last activity: [14 years, 5 months ago](https://wordpress.org/support/topic/plugin-custom-upload-dir-path-based-on-taxonomy/#post-1830227)
 * Status: resolved