在某个 cpt 更改 upload_dir 文件夹但无法更改回来
Change upload_dir folder at a certain cpt but cant change back
我尝试使用 upload_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_download"
的 cpt 上更改 'upload_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_upload_dir' 函数被调用了两次不知道为什么我也从 'admin_init' 中用函数 'call_from_admin' 调用了这个函数,到目前为止它只调用了一次很好
我去我的 CPT "rsg_download" 并且上传的文件在 wp-content/uploads/rsg-uploads/ 的正确位置,目前有效
现在我转到 "Pages" 并上传了一个文件,但我希望文件不在 /rsg-upload 中,而是在默认路径中
函数更改 upload_dir 只有当自定义 post 类型为 'rsg_download':
时才应调用此函数
function change_upload_dir( $param ) {
$mydir = '/rsg-uploads';
$param['path'] = $param['basedir'] . $mydir;
$param['url'] = $param['baseurl'] . $mydir;
return $param;
}
找到了!这只会在 "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;
}
我尝试使用 upload_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_download"
的 cpt 上更改 'upload_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_upload_dir' 函数被调用了两次不知道为什么我也从 'admin_init' 中用函数 'call_from_admin' 调用了这个函数,到目前为止它只调用了一次很好
我去我的 CPT "rsg_download" 并且上传的文件在 wp-content/uploads/rsg-uploads/ 的正确位置,目前有效
现在我转到 "Pages" 并上传了一个文件,但我希望文件不在 /rsg-upload 中,而是在默认路径中
函数更改 upload_dir 只有当自定义 post 类型为 'rsg_download':
时才应调用此函数 function change_upload_dir( $param ) {
$mydir = '/rsg-uploads';
$param['path'] = $param['basedir'] . $mydir;
$param['url'] = $param['baseurl'] . $mydir;
return $param;
}
找到了!这只会在 "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;
}