问题的实质是,我正在寻找这样的解决方案:
$blog_upload_dir_info = wp_upload_dir( $date, $blog_ID );
$blog_upload_url = $blog_upload_dir_info[ \'baseurl\' ];
在哪里
$blog_ID
与当前博客ID不同。最明显的解决方案失败:
switch_to_blog( $blog_ID );
$blog_upload_dir_info = wp_upload_dir();
restore_current_blog();
作为
wp_upload_dir() 依赖于常量
WP_CONTENT_URL
动态设置当前博客的URL,除非
upload_url_path
已设置。
当然,我可以设置这个选项,但这会将我的代码与具体的系统设置相结合,其中包括很多WTF?«-潜力。
因此,我决定虚拟添加此选项:
/**
* Apply a value to the option blog_upload_url
* if there\'s not already one
*
* @wp-hook option_upload_url_path
* @param string $upload_url
* @return string
*/
function get_real_blog_upload_url( $upload_url ) {
if ( \'\' !== trim( $upload_url ) )
return $upload_url;
$upload_path = trim( get_option( \'upload_path\' ) );
$siteurl = get_option( \'siteurl\' );
$wp_content_dir = $siteurl . \'/wp-content\';
if ( empty( $upload_path ) || \'wp-content/uploads\' == $upload_path ) {
$dir = $wp_content_dir . \'/uploads\';
} elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
// $dir is absolute, $upload_path is (maybe) relative to ABSPATH
$dir = path_join( ABSPATH, $upload_path );
} else {
$dir = $upload_path;
}
if ( empty( $upload_path ) || ( \'wp-content/uploads\' == $upload_path ) || ( $upload_path == $dir ) )
$upload_path = $wp_content_dir . \'/uploads\';
else
$upload_path = trailingslashit( $siteurl ) . $upload_path;
return $upload_path;
}
事实上,这部分是
wp_upload_dir()
因此依赖于常数
isn\'t a good practice at all. 此外,fork总是与原始实现耦合,如果原始实现发生更改,还必须修复fork。
因此,由于这个解决方案还远远不够完美,我想知道是否有更好的方法通过博客ID获取上传URL。
SO网友:Arbelzapf
为了完整起见,this 解决方案似乎一直有效,直到核心问题得到解决:
add_action(\'switch_blog\', function($new_blog, $prev_blog_id) {
update_option( \'upload_url_path\', get_option(\'siteurl\') . \'/wp-content/uploads\');
}, 10, 2);