3

I have a custom post type of 'clients' and would like all images/files uploaded through the admin for these posts to go to a specific directory on my server, but everything else to stay as normal. Is this possible maybe using wp_upload_dir?

4

You can add a filter to upload_dir. Here is a simple class I wrote to do this for a project. Use the protected $filter_path variable to define the alternate uploads directory (*will be relative to wp-content/uploads)

class UGC_Attachment {
    protected $upload_dir;
    protected $upload_url;
    protected $filter_path = '/relative_path_from_wp-content/uploads';

    function __construct() {
        $dir = wp_upload_dir();
        $this->upload_dir = $dir['basedir'] . $this->filter_path;
        $this->upload_url = $dir['baseurl'] . $this->filter_path;
    }

    function upload_dir_filter( $upload ) {
        $target_path = $this->upload_dir;
        $target_url  = $this->upload_url;
        wp_mkdir_p( $target_path );
        $upload['path'] = $target_path;
        $upload['url']  = $target_url;

       return $upload;
    }
}

Usage:

function prefix_upload_dir_filter( $post ) {
    if ( 'clients' != get_post_type( $post )
        return;
    $filter = new UGC_Attachment();
    return add_filter( 'upload_dir', array( &$filter, 'upload_dir_filter' );
}

The prefix_upload_dir_filter function would need to be attached to an action or filter with the $post object available or the $post ID. You will need to do some more research to figure this part out or maybe someone else can chime in. My usage was a complete custom image upload solution from the front end where I needed the publicly uploded images placed in a temp directory that could be cleaned out nightly via a cron job.

  • This looks perfect, will give it a try and report back later. Thanks Chris_O – Xav Apr 15 '13 at 16:37
  • I can't get this working. I got somewhere with this suggestion: [wordpress.stackexchange.com/a/77007/22448] but can't work out how to limit it to just one specific custom post type. Thanks for your help though Chris_O – Xav Apr 15 '13 at 22:29
0

Something like this may do the trick, just change mycpt as you like

add_filter('upload_dir', 'mycpt_uploaddir');

function mycpt_uploaddir( $param ){
    global $typenow;

    if ('mycpt' != $typenow)
        return $param;

    $mycpt = '/mycpt';

    $param['path'] = $param['path'] . $mycpt;
    $param['url'] = $param['url'] . $mycpt;
    return $param;
}
0

This works quite well:

add_filter( 'upload_dir', 'custom_prefix_upload_dir'  );

        function custom_prefix_upload_dir( $pathdata ) {
            global $current_user,$pagenow;
            if ( ( 'async-upload.php' == $pagenow || 'media-upload.php' == $pagenow ) && false !== strpos( wp_get_referer(),'post_type=clients' ) ) {
                $custom_dir = '/my_custom_uploads';
                if ( empty( $pathdata[ 'subdir' ] ) ) {
                    $pathdata[ 'path' ] = $pathdata[ 'path' ] . $custom_dir;
                    $pathdata[ 'url' ] = $pathdata[ 'url' ] . $custom_dir;
                    $pathdata[ 'subdir' ] = '/kanzu_intug';
                } else {
                    $new_subdir = $custom_dir . $pathdata[ 'subdir' ];

                    $pathdata[ 'path' ] = str_replace( $pathdata[ 'subdir' ], $new_subdir, $pathdata[ 'path' ] );
                    $pathdata[ 'url' ] = str_replace( $pathdata[ 'subdir' ], $new_subdir, $pathdata[ 'url' ] );
                    $pathdata[ 'subdir' ] = str_replace( $pathdata[ 'subdir' ], $new_subdir, $pathdata[ 'subdir' ] );
                }
            }

            return $pathdata;
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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