Support

Account

Home Forums General Issues Specify file upload location within uploads

Solving

Specify file upload location within uploads

  • First of all, LOVE the plugin. I don’t even know how many licenses I’ve bought for the different teams I’ve worked with over the years. Without it WordPress truly would not be as valuable as it is. Thanks Elliot!

    Background:

    I have a plugin creating a custom post type called “Sermons”.

    Within that post type I also have a custom taxonomy called “Series”. I have it set up hierarchical like a category. Here is the code:

    
    add_action( 'init', 'sermons_init' );
    function sermons_init() {
    	$labels = array(
    		'name'               => _x( 'Sermons', 'post type general name', 'pcc-sermons' ),
    		'singular_name'      => _x( 'Sermon', 'post type singular name', 'pcc-sermons' ),
    		'menu_name'          => _x( 'Sermons', 'admin menu', 'pcc-sermons' ),
    		'name_admin_bar'     => _x( 'Sermon', 'add new on admin bar', 'pcc-sermons' ),
    		'add_new'            => _x( 'Add New', 'sermon', 'pcc-sermons' ),
    		'add_new_item'       => __( 'Add New Sermon', 'pcc-sermons' ),
    		'new_item'           => __( 'New Sermon', 'pcc-sermons' ),
    		'edit_item'          => __( 'Edit Sermon', 'pcc-sermons' ),
    		'view_item'          => __( 'View Sermon', 'pcc-sermons' ),
    		'all_items'          => __( 'All Sermons', 'pcc-sermons' ),
    		'search_items'       => __( 'Search Sermons', 'pcc-sermons' ),
    		'parent_item_colon'  => __( 'Parent Sermons:', 'pcc-sermons' ),
    		'not_found'          => __( 'No sermons found.', 'pcc-sermons' ),
    		'not_found_in_trash' => __( 'No sermons found in Trash.', 'pcc-sermons' )
    	);
    
    	$args = array(
    		'labels'             => $labels,
    		'public'             => true,
    		'publicly_queryable' => true,
    		'show_ui'            => true,
    		'show_in_menu'       => true,
    		'query_var'          => true,
    		'rewrite'            => array( 'slug' => 'sermons' ),
    		'capability_type'    => 'post',
    		'has_archive'        => true,
    		'hierarchical'       => false,
    		'menu_position'      => null,
    		'supports'           => array( 'title', 'revisions' )
    	);
    
    	register_post_type( 'sermon', $args );
    }
    
    function sermon_tags_init() {
    	// create a new taxonomy
    	register_taxonomy(
    		'series',
    		'sermon',
    		array(
    			'label' 		=> __( 'Series' ),
    			'hierarchical'	=> true,
    			'rewrite' 		=> array( 'slug' => 'series' )
    		)
    	);
    
    }
    add_action( 'init', 'sermon_tags_init' );
    

    Now, I’ve used ACF to allow uploading of audio files to the custom post type “sermons”. What I’d like to do is instead of have the sermons upload to ‘uploads/year/month/day’, I’d like for them to upload the sermons/series-title/.

    I’ve got this partway there by borrowing code from this post.

    
    add_filter('wp_handle_upload_prefilter', 'spacepad_pre_upload', 2);
    add_filter('wp_handle_upload', 'spacepad_post_upload', 2);
    
    // Change the upload path to the one we want
    function spacepad_pre_upload($file){
        add_filter('upload_dir', 'spacepad_custom_upload_dir');
        return $file;
    }
    
    // Change the upload path back to the one WordPress uses by default
    function spacepad_post_upload($fileinfo){
        remove_filter('upload_dir', 'spacepad_custom_upload_dir');
        return $fileinfo;
    }
    
    function spacepad_custom_upload_dir($path){    
    	/*
         * Determines if uploading from inside a post/page/cpt - if not, default Upload folder is used
         */
        $use_default_dir = ( isset($_REQUEST['post_id'] ) && $_REQUEST['post_id'] == 0 ) ? true : false; 
        if( !empty( $path['error'] ) || $use_default_dir )
            return $path; //error or uploading not from a post/page/cpt 
    	
    	/*
    	* Save uploads in SLUG based folders 
    	*/
    	$the_cat = get_the_category( $_REQUEST['post_id'] );
    	
    	// If there is a category lets use that for our organization
    	if ( $the_cat ){
    		$customdir = '/sermons/' . strtolower(str_replace(" ", "-", $the_cat[0]->cat_name));
    	} else {
    		$post_data = get_post($_REQUEST['post_id'], ARRAY_A);
    		$slug = $post_data['post_date'];
    		
    		$customdir = '/sermons/' . $slug;	
    	}
    
        $path['path']    = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month)
        $path['url']     = str_replace($path['subdir'], '', $path['url']);      
        $path['subdir']  = $customdir;
        $path['path']   .= $customdir; 
        $path['url']    .= $customdir;  
    
        return $path;
    }
    

    As you can see, right now it uploads to sermons/$slug, where the slug is the post_date. I just need to change that slug to the series.

    I have no idea if this is possible, or if there’s a better way to do it. I’m a strong front-end developer, but my PHP is probably a 3 out of 10, so any help is appreciated.

    Thanks!

  • Did you ever solve this?

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Specify file upload location within uploads’ is closed to new replies.