WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two types of files in my WordPress Media Library, one is for pictures and attachments from posts, the other is for attachments from a custom post type.

I want to organize them separately, use my plugin's dir as basedir for the custom post type, while keep other files in Wordpress' default upload directory.

Is it possible to add one more basedir?

share|improve this question
up vote 8 down vote accepted

The following code will change the upload directory for a specific post-type!

Just be sure to swap out both instances of "post-type" (line 14) with the name of your custom post-type.

 * Change Upload Directory for Custom Post-Type
 * This will change the upload directory for a custom post-type. Attachments will
 * now be uploaded to an "uploads" directory within the folder of your plugin. Make
 * sure you swap out "post-type" in the if-statement with the appropriate value...
function custom_upload_directory( $args ) {

    $id = $_REQUEST['post_id'];
    $parent = get_post( $id )->post_parent;

    // Check the post-type of the current post
    if( "post-type" == get_post_type( $id ) || "post-type" == get_post_type( $parent ) ) {
        $args['path'] = plugin_dir_path(__FILE__) . "uploads";
        $args['url']  = plugin_dir_url(__FILE__) . "uploads";
        $args['basedir'] = plugin_dir_path(__FILE__) . "uploads";
        $args['baseurl'] = plugin_dir_url(__FILE__) . "uploads";
    return $args;
add_filter( 'upload_dir', 'custom_upload_directory' );

This was made to work with plugins, but could be modified for use in themes. Hope this helps, let me know if you have any questions!

== UPDATE ==

I forgot to mention, if you'd like to use WordPress' default year/month method of organizing the uploads folder, simply change lines 15 and 16 to the following:

$args['path'] = plugin_dir_path(__FILE__) . "uploads" . $args['subdir'];
$args['url']  = plugin_dir_url(__FILE__) . "uploads" . $args['subdir'];
share|improve this answer
thanks, jhdenham! your code changes the subdir which is very helpful. But I want to add one more basedir which is stored in wp site options. Just edited my question to make it clear. – Jenny Dec 6 '11 at 23:52
Glad to help! Assuming your custom post-type is being created from a plugin, the code above will create a separate "uploads" directory within the directory of your plugin. Isn't that what you were looking to do? Sorry if this still doesn't help! – jhdenham Dec 7 '11 at 4:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.