我想每次我上传一个PDF文件到我的网站,而不是默认的WordPress路径,我的文件将保存在不同的路径。
为此,我在functions.php
文件中放置了以下过滤器:
function custom_upload_directory( $file ) {
// Check if the file is a PDF
if ( $file['type'] == 'application/pdf' ) {
// Set the upload directory to a custom location
$file['url'] = '/wp-content/uploads/test-for-pdf/' . $file['name'];
$file['error'] = false;
}
return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'custom_upload_directory' );
字符串
通过放置此过滤器,PDF文件在WordPress中正确上传,但它保存在其默认路径中,并且没有文件上传到我想要的路径中。
2条答案
按热度按时间af7jpaap1#
你可以试试下面的方法,虽然我没有测试过。
字符串
z5btuh9x2#
从我对WP上的wp_handle_upload_prefilter页面的阅读来看(我不使用),你没有得到改变过滤器路径的选项,只有名称。
默认位置是在引导过程中defined,我建议不要乱用它,而是使用插件,或者写一个cron脚本来移动文件。
请参阅:https://devowl.io/knowledge-base/real-physical-media-change-wp-content-uploads-folder/