im try something with the upload_dir filter

I check the current CPT with this function

    function get_current_post_type() {
    global $post, $typenow, $current_screen;

    //we have a post so we can just get the post type from that
    if ( $post && $post->post_type ) {
        return $post->post_type;
    } //check the global $typenow - set in admin.php
    elseif ( $typenow ) {
        return $typenow;
    } //check the global $current_screen object - set in sceen.php
    elseif ( $current_screen && $current_screen->post_type ) {
        return $current_screen->post_type;
    } //lastly check the post_type querystring
    elseif ( isset( $_REQUEST['post_type'] ) ) {
        return sanitize_key( $_REQUEST['post_type'] );
    }

    //we do not know the post type!
    return NULL;
}

now i want to change the ‘upload_dir’ on at a certain cpt called “rsg_download”

add_action( 'admin_init', 'call_from_admin' );
function call_from_admin() {
   //Here i get the Current custom Post type is the Post type = "rsg_download" then i want upload in a other folder called "rsg-uploads"
    $currentCPT = get_current_post_type();
    if ( $currentCPT === 'rsg_download' ) {
        add_filter( 'upload_dir', 'change_upload_dir' );
    }
}

When i use only

$currentCPT = get_current_post_type();
if ( $currentCPT === 'rsg_download' ) {
    add_filter( 'upload_dir', 'change_upload_dir' );
}

The ‘change_upload_dir’ function is called twice dont know why also i call this from ‘admin_init’ with the function ‘call_from_admin’ and it calls only one time so far so good

i go to my CPT “rsg_download” and the uploade files are in the right place at wp-content/uploads/rsg-uploads/ this works so far

now i go to “Pages” and i upload a File but i want the files not in /rsg-upload but in the default path

Function to change the upload_dir this function should only be called when the custom post type is ‘rsg_download’:

  function change_upload_dir( $param ) {

    $mydir="/rsg-uploads";
    $param['path'] = $param['basedir'] . $mydir;
    $param['url']  = $param['baseurl'] . $mydir;
    return $param;
}

got some help from #wordpress in freenode but still not working for me :/
Current code i try the upload folder is still the standard one:

    function get_current_post_type() {
    global $post, $typenow, $current_screen;

    if ( $post && $post->post_type )
        return $post->post_type;

    elseif( $typenow )
        return $typenow;

    elseif( $current_screen && $current_screen->post_type )
        return $current_screen->post_type;

    elseif( isset( $_REQUEST['post_type'] ) )
        return sanitize_key( $_REQUEST['post_type'] );

    return null;
}

function custom_post_type_upload_directory( $args ) {

    if( 'rsg_download' == get_current_post_type() ) {
        $mydir="/rsg-uploads";
        $args['path']   = $args['basedir'] . $mydir;
        $args['url']    = $args['baseurl'] . $mydir;
    }

    return $args;
}

add_filter( 'upload_dir', 'custom_post_type_upload_directory' );

3 Answers
3

I found! this will only change the upload dir when upload in the “rsg_download” CPT

add_filter( 'wp_handle_upload_prefilter', 'rsg_pre_upload' );
function rsg_pre_upload( $file ) {
    add_filter( 'upload_dir', 'rsg_custom_upload_dir' );
    return $file;
}

function rsg_custom_upload_dir( $param ) {
    $id = $_REQUEST['post_id'];
    $parent = get_post( $id )->post_parent;
    if( "rsg_download" == get_post_type( $id ) || "rsg_download" == get_post_type( $parent ) ) {
        $mydir="/rsg-uploads";
        $param['path'] = $param['basedir'] . $mydir;
        $param['url']  = $param['baseurl'] . $mydir;
    }
    return $param;


}

i got some trouble when i used any Framework for creating Metaboxes i tried Vafpress Redux CBM2 but the Problem was on my side

here is how i got working for any custom upload fields

function rsg_custom_upload_dir( $param ) {

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



if ( "rsg_download" == get_post_type( $id ) || "rsg_download" == get_post_type( $parent ) ) {
    $mydir="/rsg_uploads";
    $param['path'] = $param['basedir'] . $mydir;
    $param['url']  = $param['baseurl'] . $mydir;

} elseif ( strpos( $current_page, 'rsg_download' ) ) {
    $mydir="/rsg_uploads";
    $param['path'] = $param['basedir'] . $mydir;
    $param['url']  = $param['baseurl'] . $mydir;

}

return $param;

}

Leave a Reply

Your email address will not be published. Required fields are marked *