您可以使用Joost提供的想法并使用upload_dir
筛选以临时将上载路径设置为其他位置。
/**
* Override the default upload path.
*
* @param array $dir
* @return array
*/
function wpse_141088_upload_dir( $dir ) {
return array(
\'path\' => $dir[\'basedir\'] . \'/mycustomdir\',
\'url\' => $dir[\'baseurl\'] . \'/mycustomdir\',
\'subdir\' => \'/mycustomdir\',
) + $dir;
}
然后在实践中,很容易做到:
// Register our path override.
add_filter( \'upload_dir\', \'wpse_141088_upload_dir\' );
// Do our thing. WordPress will move the file to \'uploads/mycustomdir\'.
$result = wp_handle_upload( $_FILE[\'myfile\'] );
// Set everything back to normal.
remove_filter( \'upload_dir\', \'wpse_141088_upload_dir\' );