wordpress默认上传图片(文件)路径是uploads目录的‘年/月’文件夹,我们可以通过upload_dir钩子改变默认上传路径,自定义上传路径,该函数指定了uploads的目录为uploads的avatar文件夹,代码如下:
- function my_upload_dir($upload) {
- $upload['subdir']= '/avatar';
- $upload['path']= $upload['basedir'] . $upload['subdir'];
- $upload['url']= $upload['baseurl'] . $upload['subdir'];
- return $upload;
- }
然后在需要改变上传路径的地方加上钩子函数,代码如下:
- //Handles PHP uploads in WordPress.
- add_filter('upload_dir', 'my_upload_dir');
- $file_attr = wp_handle_upload( $_FILES['avatar_manager_import'], $overrides );
- remove_filter('upload_dir', 'my_upload_dir');
当然上传图片和文件需要调用wordpress的上传图片函数.
新闻热点
疑难解答
图片精选