在某个CPT更改Upload_dir文件夹,但不能更改回原来的位置

时间:2015-03-06 作者:cannap

im尝试使用upload\\U dir筛选器

我使用此功能检查当前CPT

    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;
}
现在,我想在名为“rsg\\U下载”的特定cpt上更改“upload\\u dir”

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\' );
    }
}
当我只使用

$currentCPT = get_current_post_type();
if ( $currentCPT === \'rsg_download\' ) {
    add_filter( \'upload_dir\', \'change_upload_dir\' );
}
“change\\u upload\\u dir”函数被调用了两次不知道为什么我也用“call\\u from\\u admin”函数从“admin\\u init”调用这个函数,到目前为止它只调用了一次很好

我转到我的CPT“rsg\\u下载”,上传文件在wp-content/uploads/rsg-uploads/this-works中的正确位置

现在我转到“页面”并上载文件,但我希望文件不在/rsg上载中,而是在默认路径中

函数更改upload\\u目录只有在自定义帖子类型为“rsg\\u download”时才应调用此函数:

  function change_upload_dir( $param ) {

    $mydir = \'/rsg-uploads\';
    $param[\'path\'] = $param[\'basedir\'] . $mydir;
    $param[\'url\']  = $param[\'baseurl\'] . $mydir;
    return $param;
}
在freenode中从#wordpress获得了一些帮助,但仍然不适用于我:/Current code我尝试上载文件夹仍然是标准文件夹:

    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 个回复
最合适的回答,由SO网友:cannap 整理而成

我找到了!这只会在“rsg\\U下载”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;


}
当我使用任何框架来创建元盒时,我遇到了一些麻烦。我尝试了Vafpress Redux CBM2,但问题出在我这边

下面是我如何为任何自定义上载字段工作的

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;

}

SO网友:Dave Ross

在此代码中:

if ( $currentCPT = \'rsg_download\' ) {
您正在分配值\'rsg_download\'$currentCPT. 在if()中执行赋值时,if()只会看到您指定的值。由于非空字符串始终为true,因此if()的主体始终执行。要测试该值,需要使用== 甚至更好=== (参见https://stackoverflow.com/questions/2063480/the-3-different-equals?rq=1).

然而,一旦你打过电话add_filter, 它将在每次为页面的其余部分调用过滤器时运行您的函数。如果在页面上的多个位置需要上载目录,则可能会多次调用它。

您需要将CPT支票移到change_upload_dir 功能:

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;

}
然后您可以去掉add\\u过滤器周围的if(),让它一直被调用。

SO网友:David

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;

}
结束