您可以添加一个筛选器以上载\\u dir。这是我为一个项目编写的一个简单类。使用protected $filter_path
变量定义备用上载目录(*将与wp内容/上载相关)
class UGC_Attachment {
protected $upload_dir;
protected $upload_url;
protected $filter_path = \'/relative_path_from_wp-content/uploads\';
function __construct() {
$dir = wp_upload_dir();
$this->upload_dir = $dir[\'basedir\'] . $this->filter_path;
$this->upload_url = $dir[\'baseurl\'] . $this->filter_path;
}
function upload_dir_filter( $upload ) {
$target_path = $this->upload_dir;
$target_url = $this->upload_url;
wp_mkdir_p( $target_path );
$upload[\'path\'] = $target_path;
$upload[\'url\'] = $target_url;
return $upload;
}
}
用法:
function prefix_upload_dir_filter( $post ) {
if ( \'clients\' != get_post_type( $post )
return;
$filter = new UGC_Attachment();
return add_filter( \'upload_dir\', array( &$filter, \'upload_dir_filter\' );
}
prefix\\u upload\\u dir\\u filter函数需要附加到具有可用$post对象或$post ID的操作或筛选器。您需要做更多的研究来找出这一部分,或者其他人可以插话。我的使用是一个完整的定制图像上传解决方案,从前端开始,我需要将公开上传的图像放置在临时目录中,该目录可以通过cron作业每晚清除。