WP Mayor is supported by its audience. Our recommendations are based on our experts’ research and hands-on testing. If you purchase through a link on our website, we may earn a commission. Here’s why you can trust us.

How to Set Upload Directory for a Custom Post Type

  Introduction
I recently came across a very nifty piece of code (credits to jdenham) for setting the upload directory for a custom post type.
WP Mayor is supported by its audience. Our recommendations are based on our experts’ research and hands-on testing. If you purchase through a link on our website, we may earn a commission. Here’s why you can trust us.
 Table of contents

I recently came across a very nifty piece of code (credits to jdenham) for setting the upload directory for a custom post type:

[php]
/**
* 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’ );
[/php]

If you enjoyed this post, make sure to subscribe to WPMayor’s RSS feed.

Jean Galea
Jean Galea
Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.
Recommended For You
Recommended For You
Get more expert advice like this delivered to your inbox every week.
Join thousands of designers, developers, and marketers who trust our experts to find and test the best tools and services for your website.
By signing up to our newsletter, you agree to receive electronic communications from WP Mayor that may include advertisements.
Get more expert advice like this delivered to your inbox every week.
Join thousands of designers, developers and marketers who trust our experts to find and test the best tools to build, improve, and optimize their websites.
By signing up to our newsletter, you agree to receive electronic communications from WP Mayor that may include advertisements.
Join the Conversation

4 Responses

  1. Based on what you did, here’s what I’ve done to create a directory for each custom post type.
    Note that there’s a little hack (str_replace()) because of wp-less plugin.

    function custom_upload_directory( $args ) {
    $unaltered_post_type = array( ‘page’, ‘post’ );
    if ( !isset( $_REQUEST[‘post_type’] ) ) {
    if ( isset( $_REQUEST[‘post’] ) ) {
    $post_type = get_post_type( $_REQUEST[‘post’] );
    } elseif ( isset( $_REQUEST[‘post_id’] ) ) {
    $post_type = get_post_type( $_REQUEST[‘post_id’] );
    } else {
    $post_type = ”;
    }
    } else {
    $post_type = $_REQUEST[‘post_type’];
    }

    if ( !empty( $post_type ) && !in_array( $post_type, $unaltered_post_type ) ) {
    $args[‘basedir’] = str_replace(‘/wp-less’, ”, $args[‘basedir’]);
    $args[‘baseurl’] = str_replace(‘/wp-less’, ”, $args[‘baseurl’]);
    $args[‘path’] = $args[‘basedir’] . ‘/’ . $post_type;
    $args[‘url’] = $args[‘baseurl’] . ‘/’ . $post_type;
    $args[‘subdir’] = $post_type;
    }
    return $args;
    }
    add_filter( ‘upload_dir’, ‘custom_upload_directory’ );

  2. Awesome, nice trick!

    I tried to modify it a bit to work on a regular plugin. In this example I did a little plugin to register members of some kind and have the profile photos upload to the plugin dir just like in the original. Although this doesn’t seem to work, the files are still saved in the regular dir. Any ideas?

    The plugin itself uses a simple form to save data to db and I also hook the wp-uploader to upload and send the image url with the rest of the data.

    function custom_upload_directory( $args ) {

    $screen = get_current_screen();

    // If the parent_base is members, upload to plugin directory
    if ( $screen->parent_base == ‘members’ ) {
    $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’ );

Leave a Reply

Your email address will not be published.

From web hosting to WooCommerce plugins and backup services, we have put together a collection of exclusive coupons and deals just for you.
WP Mayor_Icon_white

Reader Survey

Supercharge your WooCommerce store!