wp_upload_bits() 使用wp_upload_dir() 激发了upload_dir 过滤器允许您修改上载目录的路径、子目录和URL。
所以你可以用这个过滤器来“强制”wp_upload_bits() 要使用自定义路径,请执行以下操作:
// Demo to get the image data.
$url = \'https://logo.clearbit.com/starcomww.com\';
$content = file_get_contents( $url );
$_filter = true; // For the anonymous filter callback below.
add_filter( \'upload_dir\', function( $arr ) use( &$_filter ){
    if ( $_filter ) {
        $folder = \'/org_logos\'; // No trailing slash at the end.
        $arr[\'path\'] .= $folder;
        $arr[\'url\'] .= $folder;
        $arr[\'subdir\'] .= $folder;
    }
    return $arr;
} );
$filename = wp_basename( $url ) . \'.png\';
$ret = wp_upload_bits( $filename, null, $content );
var_dump( $ret ); // For testing.
$_filter = false; // Disables the filter.
 也可以使用命名回调:
function my_org_logos_upload_dir( $arr ) {
    $folder = \'/org_logos\'; // No trailing slash at the end.
    $arr[\'path\'] .= $folder;
    $arr[\'url\'] .= $folder;
    $arr[\'subdir\'] .= $folder;
    return $arr;
}
// Demo to get the image data.
$url = \'https://logo.clearbit.com/starcomww.com\';
$content = file_get_contents( $url );
add_filter( \'upload_dir\', \'my_org_logos_upload_dir\' );
$filename = wp_basename( $url ) . \'.png\';
$ret = wp_upload_bits( $filename, null, $content );
var_dump( $ret ); // For testing.
remove_filter( \'upload_dir\', \'my_org_logos_upload_dir\' );
 更新(回复您的评论)
因此,作为过滤器wp_upload_dir(), 这将适用于通过wp_upload_dir(), 是这样吗?
是的,会的。
但是,只有在调用时添加/启用筛选器,才能防止此问题wp_upload_bits() and 您希望它使用您的自定义上载路径,然后在应用过滤器后删除/禁用过滤器-以防止在以下情况下更改上载路径:wp_upload_dir() 从其他函数或代码调用。
因此,正如您在原始答案中所看到的,我的方法如下:
// First variant of the code - using an anonymous function.
$_filter = true;  // enables the filter
$_filter = false; // disables the filter
// Second variant of the code - using a named function.
remove_filter( \'upload_dir\', \'my_org_logos_upload_dir\' ); // enables the filter
add_filter( \'upload_dir\', \'my_org_logos_upload_dir\' );    // disables the filter
 第二个属性
wp_insert_attachment() 是
$filename,  
$content 或某些属性
$ret[here]?
的第二个参数wp_insert_attachment() 函数是要附加到帖子的文件的完整绝对路径。
一旦成功,wp_upload_bits() 返回具有file 作为键之一,其中的值是由保存的文件的完整绝对路径wp_upload_bits(). (您可以检查函数的reference 或this note 对于其他键。)
所以答案是$ret[\'file\']:
wp_insert_attachment( array(...), $ret[\'file\'] );