WordPress默认的上传路径为wp-content\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\uploads,并且以年和月文件夹归档。那么WordPress如何修改附件上传默认的存放目录?我们一起了解一下。

方法一

通过add_filter 钩子修改上传路径

将下面的代码添加到当前主题functions.php文件中:

function slider_upload_dir($uploads) {

$siteurl = get_option( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’siteurl\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ );

$uploads[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’path\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] = WP_CONTENT_DIR . \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’/slider\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;

$uploads[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’url\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] = $siteurl . \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’/wp-content/slider\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;

$uploads[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’subdir\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;

$uploads[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’basedir\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] = $uploads[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’path\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’];

$uploads[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’baseurl\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] = $uploads[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’url\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’];

$uploads[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’error\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] = false;

return $uploads;

}

add_filter(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’upload_dir\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’slider_upload_dir\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’);

之后附件上传路径会修改为:wp-content/slider,自己可以修改目录名称。

需要注册的是使用该方法修改路径后,如果再次修改路径,在后台媒体库中将看不到之前上传的图片。

方法二

找回WordPress隐藏的上传路径选项。

自WordPress 3.5版本开始,隐藏了后台媒体设置页面的“默认上传路径和文件的完整URL地址”选项,可以通过下面的代码将该选项调出来。

将下面的代码添加到当前主题functions.php文件中,就可以调出该选项:

if(get_option(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’upload_path\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’)==\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’wp-content/uploads\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ || get_option(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’upload_path\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’)==null) {

update_option(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’upload_path\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,WP_CONTENT_DIR.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’/uploads\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’);

}

该代码使用后删除即可,不用保留在主题中。

需要注意的是,上传路径只能写相对路径,例如:wp-content/uploads/img

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部