WordPress.org

Ready to get started?Download WordPress

Forums

Custom Upload Dir
[resolved] Path based on Taxonomy? (5 posts)

  1. cmurockstar
    Member
    Posted 3 years ago #

    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/

  2. cmurockstar
    Member
    Posted 3 years ago #

    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.

  3. ulfben
    Member
    Plugin Author

    Posted 3 years ago #

    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?

  4. cmurockstar
    Member
    Posted 3 years ago #

    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

  5. ulfben
    Member
    Plugin Author

    Posted 2 years ago #

    This is in version 3.3 btw. Sorry for not updating the thread. You can use %category%, %tags% or any of your own custom taxonomies straight up.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic