0

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' );
2

In this code:

if ( $currentCPT = 'rsg_download' ) {

you're assigning the value 'rsg_download' to $currentCPT. When you do an assignment inside an if(), the if() only sees the value you assigned. Since a non-empty string is always true, the body of the if() always executes. To test the value instead, you need to use == or even better === (see https://stackoverflow.com/questions/2063480/the-3-different-equals?rq=1).

However, once you've called add_filter, it will run your function every time that filter is called for the rest of the page. And it may be called multiple times if the upload directory is needed in more than one place on the page.

You need to move your CPT check inside your change_upload_dir function:

function change_upload_dir( $param ) {

    $currentCPT = get_current_post_type();
    if ( $currentCPT === 'rsg_download' ) {

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

    }

    return $param;

}

Then you can get rid of the if() around add_filter and let it be called all the time.

  • Hi, thx my code looks now so laravel.io/bin/GyjWm but the dir is still wp-content/uploads/2015/03/.... and not wp-content/uploads/rsg-uploads :/ – cannap Mar 6 '15 at 10:44
  • i got some help in a irc channel but this is still not working the upload_dir is the Standard for the cpt and not /rsg-uploads laravel.io/bin/ro0Pv – cannap Mar 6 '15 at 12:09
1

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;

}
1

Based on the above solution, this one works with CPT and uploads based on Y/M structures:


    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( "artwork" == get_post_type( $id ) || "artwork" == get_post_type( $parent ) ) {

    $param['subdir']  = '/artworks' . $param['subdir'];
    $param['path'] = $param['basedir'] . $param['subdir'];
    $param['url']  = $param['baseurl'] . $param['subdir'];
    }
    return $param;

}

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.