1

我刚刚安装了一个带有映射域的 wordpress 多站点安装(4.2.1 版)。

对于每个站点,浏览器中显示的上传图像的 url 是http://URL/wp-content/uploads/sites/SITENUMBER/DATE/...,文件夹是/path/to/wordpress/wp-content/uploads/sites/SITENUMBER/DATE/.... 但我想使用本地路径/path/to/wordpress/wp-content/uploads/sites/SITENUMBER/...并想在浏览器中查看http://URL/wp-content/uploads/DATE/...(例如在单个 wordpress 站点中)。

我已经阅读了很多关于此的内容,我想我知道“上传 URL 路径”选项通常应该重写它,不是吗?但对我来说它不起作用。我如何自定义 wordpress 以适应它?

更新:我发现在 functions.php 中定义了多站点将重写 URL:

// 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 ms-files rewriting is disabled (networks created post-3.5), it is fairly
* straightforward: Append sites/%d if we're not on the main site (for post-MU
* networks). (The extra directory prevents a four-digit ID from conflicting with
* a year-based directory for the main site. But if a MU-era network has disabled
* ms-files rewriting manually, they don't need the extra directory, as they never
* had wp-content/uploads for the main site.)
*/

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

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

但我不知道如何改变它以获得所需的行为。

4

1 回答 1

1

是的,您可以使用以下功能在 WordPress 中更改您的上传目录。

add_filter( 'upload_dir', 'upload_dir_filter' );

function upload_dir_filter( $upload ) {
    $dir = $_SERVER['DOCUMENT_ROOT'];
    $upload_dir = $dir['basedir'] . '/uploads/date';
    $upload_url = $dir['baseurl'] . '/uploads/date';

    wp_mkdir_p( $upload_dir );  //WordPress will check if the dir exists and can write to it.
    $upload['path'] = $upload_dir;
    $upload['url']  = $upload_url;

        return $upload;
    }
于 2015-04-29T10:55:46.243 回答