最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Shared upload folder in wordpress multisite

programmeradmin0浏览0评论

I have a wordpress multisite, where I would like all the images uploaded through the main site (mysite) to be accessible to sub1.mysite and sub2.mysite. All the images should be in one folder without subdirectory (e.g: wp-content/uploads/image.jpg). I tried the bellow function, but my images for subdomains are uploaded to 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' );

I have a wordpress multisite, where I would like all the images uploaded through the main site (mysite) to be accessible to sub1.mysite and sub2.mysite. All the images should be in one folder without subdirectory (e.g: wp-content/uploads/image.jpg). I tried the bellow function, but my images for subdomains are uploaded to 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' );
Share Improve this question edited Jun 6, 2014 at 8:32 kraftner 5,6281 gold badge29 silver badges46 bronze badges asked Jun 6, 2014 at 7:34 TotemTotem 231 silver badge5 bronze badges 2
  • In MultiSite, wp_upload_dir appends the site-specific sub-directory directly on the path argument. subdir will be empty (or the year/month), so str_replace'ing it will have no effect. Do you want all uploads in the root folder, or just for the main site? – TheDeadMedic Commented Jun 6, 2014 at 8:50
  • Thanks TheDeadMedic. Yes I want all uploads in one folder. – Totem Commented Jun 6, 2014 at 21:42
Add a comment  | 

1 Answer 1

Reset to default 7

This will force uploads for all sites to the wp-content/uploads directory. Sub-directories (like year/month) will still exist (if the setting is enabled).

/**
 * Force all network uploads to reside in "wp-content/uploads", and by-pass
 * "files" URL rewrite for site-specific directories.
 * 
 * @link    http://wordpress.stackexchange/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' );
发布评论

评论列表(0)

  1. 暂无评论