用于在多站点中更改wp_pload_dir()路径的挂钩筛选器

时间:2017-03-30 作者:TorryVic

上载目录和URL路径存储在选项数据库中。您可以使用

update_option():

update_option( \'upload_path\', ABSPATH . \'/path/to/uploads\' );
update_option( \'upload_path_url\', site_url( \'/uploads/\' ) );
但是,如果它是network multisite上的博客站点,则函数_wp_upload_dir 在…上/wp-includes/functions.php 在upload\\u path和upload\\u path\\u url上定义的url之后添加/site/id\\u blog/。

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,
    );
  }
所以我只需要删除下一个条件:

  if ( defined( \'MULTISITE\' ) )
        $ms_dir = \'/sites/\' . get_current_blog_id();
  else
        $ms_dir = \'/\' . get_current_blog_id();
对此:

      $ms_dir = \'/\'
但我不想弄乱核心的wordpress代码。如果可能的话,我想用过滤器挂钩来更换。

任何建议都会很有帮助。

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

使用upload_dir 滤器

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;
}