WordPress MultiSite中的共享上载文件夹

时间:2014-06-06 作者:Totem

我有一个wordpress多站点,我希望通过主站点(mysite.com)上传的所有图片都可以访问sub1.mysite.comsub2.mysite.com. 所有图像应位于一个没有子目录的文件夹中(例如:wp-content/uploads/image.jpg).我尝试了下面的函数,但我的子域图像上载到wp-content/uploads/sites/#blog_id/

function wpse_16722_upload_dir( $args ) {
    $newdir = \'/\';

    $args[\'path\']    = str_replace( $args[\'subdir\'], \'\', $args[\'path\'] ); //remove default subdir
    $args[\'url\']     = str_replace( $args[\'subdir\'], \'\', $args[\'url\'] );      
    $args[\'subdir\']  = $newdir;
    $args[\'path\']   .= $newdir; 
    $args[\'url\']    .= $newdir; 

    return $args;
}
add_filter( \'upload_dir\', \'wpse_16722_upload_dir\' );

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

这将强制将所有站点上载到wp-content/uploads 目录子目录(如year/month) 仍将存在(如果启用该设置)。

/**
 * Force all network uploads to reside in "wp-content/uploads", and by-pass
 * "files" URL rewrite for site-specific directories.
 * 
 * @link    http://wordpress.stackexchange.com/q/147750/1685
 * 
 * @param   array   $dirs
 * @return  array
 */
function wpse_147750_upload_dir( $dirs ) {
    $dirs[\'baseurl\'] = network_site_url( \'/wp-content/uploads\' );
    $dirs[\'basedir\'] = ABSPATH . \'wp-content/uploads\';
    $dirs[\'path\'] = $dirs[\'basedir\'] . $dirs[\'subdir\'];
    $dirs[\'url\'] = $dirs[\'baseurl\'] . $dirs[\'subdir\'];

    return $dirs;
}

add_filter( \'upload_dir\', \'wpse_147750_upload_dir\' );

结束