3

The upload directory and URL paths are stored in the options database. You can change them using

update_option():

update_option( 'upload_path', ABSPATH . '/path/to/uploads' );
update_option( 'upload_path_url', site_url( '/uploads/' ) );

However, if it's a blog site on network multisite, the function _wp_upload_dir on /wp-includes/functions.php add /site/id_blog/ after the url defined on upload_path and upload_path_url.

function _wp_upload_dir( $time = null ) {
      $siteurl = get_option( 'siteurl' );
      $upload_path = trim( get_option( 'upload_path' ) );

    if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
        $dir = WP_CONTENT_DIR . '/uploads';
    } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
        $dir = path_join( ABSPATH, $upload_path );
    } else {
        $dir = $upload_path;
    }

    if ( !$url = get_option( 'upload_url_path' ) ) {
        if ( empty($upload_path) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) )
              $url = WP_CONTENT_URL . '/uploads';
        else
            $url = trailingslashit( $siteurl ) . $upload_path;
    }

    if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) {
        $dir = ABSPATH . UPLOADS;
        $url = trailingslashit( $siteurl ) . UPLOADS;
    }

    // If multisite (and if not the main site in a post-MU network)
    if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {

        if ( ! get_site_option( 'ms_files_rewriting' ) ) {

              if ( defined( 'MULTISITE' ) )
                $ms_dir = '/sites/' . get_current_blog_id();
            else
                $ms_dir = '/' . get_current_blog_id();

            $dir .= $ms_dir;
            $url .= $ms_dir;

        } elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) {
            if ( defined( 'BLOGUPLOADDIR' ) )
                $dir = untrailingslashit( BLOGUPLOADDIR );
            else
                $dir = ABSPATH . UPLOADS;
              $url = trailingslashit( $siteurl ) . 'files';
        }
    }

    $basedir = $dir;
    $baseurl = $url;

    $subdir = '';
    if ( get_option( 'uploads_use_yearmonth_folders' ) ) {
        if ( !$time )
            $time = current_time( 'mysql' );
        $y = substr( $time, 0, 4 );
        $m = substr( $time, 5, 2 );
        $subdir = "/$y/$m";
    }

    $dir .= $subdir;
    $url .= $subdir;

    return array(
        'path'    => $dir,
        'url'     => $url,
        'subdir'  => $subdir,
        'basedir' => $basedir,
        'baseurl' => $baseurl,
        'error'   => false,
    );
  }

So I need to delete only next condition:

  if ( defined( 'MULTISITE' ) )
        $ms_dir = '/sites/' . get_current_blog_id();
  else
        $ms_dir = '/' . get_current_blog_id();

To this:

      $ms_dir = '/'

But I don't like to mess up the core wordpress code. I would like change it by a filter hook if it's possible.

Any advice would be helpful.

3

Use the upload_dir filter.

add_filter( 'upload_dir', 'wpse_261931_upload_dir', 10, 1 );
function wpse_261931_upload_dir( array $uploads ) {
  if( is_multisite() ) {
    //* Do something interesting with the $uploads directory

  }
  return $uploads;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.