如何更改wordpress网站文件的存储路径

 火... [复制链接]  322查看
易西 发表于 2023-10-16 20:01:56 | 显示全部楼层 |阅读模式
如何更改wordpress网站文件的存储路径
 楼主| 易西 发表于 2023-10-16 22:53:03 | 显示全部楼层
https://www.cnwper.com/wpchange-uploadsurl.html
参考


  1. function change_upload_dir($uploads) {
  2.     $siteurl = get_option( 'siteurl' );
  3.     $uploads['path'] = WP_CONTENT_DIR . '/newurl';
  4.     $uploads['url'] = $siteurl . '/wp-content/newurl';
  5.     $uploads['subdir'] = '';
  6.     $uploads['basedir'] = $uploads['path'];
  7.     $uploads['baseurl'] = $uploads['url'];
  8.     $uploads['error'] = false;
  9.     return $uploads;
  10.   }
  11.   add_filter('upload_dir', 'change_upload_dir');
复制代码

 楼主| 易西 发表于 2023-10-16 20:04:26 | 显示全部楼层
[发帖际遇]: 一个袋子砸在了 易西 头上,易西 赚了 5 金钱. 幸运榜 / 衰神榜
 楼主| 易西 发表于 2023-10-16 20:11:36 | 显示全部楼层
本帖最后由 易西 于 2023-10-16 21:14 编辑

https://js.aizhan.com/cms/wordpress/22521.html
参考
存储路径
WordPress上传文件都是存放在默认目录的,如果你想更改存储路径却不知道怎么操作的话,不妨来看看爱站技术频道小编所整理的资料,相信对你一定会有所帮助的。
有时候将不同类型的文件分门别类存储,似乎比年月目录更有意义。例如幻灯片应该存储在slides目录下,下载文件应该存储在downloads文件夹下。就说幻灯片slideshow,我比较喜欢用自定义文章类型(Custom Post Type)实现,有些幻灯片脚本比较个性,不支持绝对路径,必须用相对路径,然后用base参数设置相对于哪个文件夹,这样幻灯片必须存储在某个特定的文件夹中,年月形式显然不满足要求。所以,我们需要条件化的设置上传目录。

一、为Custom Post Type设置上传目录

假设我要将所有在幻灯片类型的文章中上传的文件存储到/wp-content/uploads/slides文件夹中,将下面的代码放到主题的functions.php中即可

代码如下:
  1. function custom_upload_directory( $uploads ) {
  2. $id = $_REQUEST['post_id'];
  3. $parent = get_post( $id )->post_parent;
  4. if( "post-type" == get_post_type( $id ) || "post-type" == get_post_type( $parent ) ) {
  5. $subdir = 'slides';
  6. $uploads['subdir'] = $subdir;
  7. $uploads['path'] = $uploads['basedir'].DIRECTORY_SEPARATOR.$subdir;
  8. $uploads['url'] = $uploads['baseurl'].'/'.$subdir;
  9. }
  10. return $uploads;
  11. }
  12. add_filter( 'upload_dir', 'custom_upload_directory' );

复制代码

将post-type替换成自己的自定义文章类型名称,将你要创建的子目录赋值给$subdir。

二、将文件保存到插件目录

下面的代码要用在插件中,文件会保存到插件目录下的uploads文件夹下。

代码如下:

/**
* Change Upload Directory for Custom Post-Type
*
* This will change the upload directory for a custom post-type. Attachments will
* now be uploaded to an "uploads" directory within the folder of your plugin. Make
* sure you swap out "post-type" in the if-statement with the appropriate value...
*/
function custom_upload_directory( $args ) {
$id = $_REQUEST['post_id'];
$parent = get_post( $id )->post_parent;
// Check the post-type of the current post
if( "post-type" == get_post_type( $id ) || "post-type" == get_post_type( $parent ) ) {
$args['path'] = plugin_dir_path(__FILE__) . "uploads";
$args['url'] = plugin_dir_url(__FILE__) . "uploads";
$args['basedir'] = plugin_dir_path(__FILE__) . "uploads";
$args['baseurl'] = plugin_dir_url(__FILE__) . "uploads";
}
return $args;
}
add_filter( 'upload_dir', 'custom_upload_directory' );

如果要以年月形式保存,修改一下代码即可

代码如下:
$args['path'] = plugin_dir_path(__FILE__) . "uploads" . $args['subdir'];
$args['url'] = plugin_dir_url(__FILE__) . "uploads" . $args['subdir'];


三、为后台管理页面设定upload_dir
用wp_editor在后台管理页面(比如用add_menu_page创建的页面)创建一个媒体上传功能,希望所有从该页面上传的文件都保存到wp-content/uploads/myfolder目录下。
由 于ajax上传是直接调用wp-admin/async_upload.php文件,只能通过post_id获取post信息,而后台管理页面并非 post,所以判断什么时候应该更改upload_dir有些麻烦。此时,可以用采用判断页面referer的方法,用wp_get_referer() 函数获取引荐url,如果正好与我们的option page url想等,就更该目录。

代码如下:
  1. function custom_upload_directory( $uploads ) {
  2. if( wp_get_referer() == 'http://domain.com/wp-admin/admin.php?page=myoptionpage'){
  3. $subdir = 'myfolder';
  4. $uploads['subdir'] = $subdir;
  5. $uploads['path'] = $uploads['basedir'].DIRECTORY_SEPARATOR.$subdir;
  6. $uploads['url'] = $uploads['baseurl'].'/'.$subdir;
  7. }
  8. return $uploads;
  9. }
  10. add_filter( 'upload_dir', 'custom_upload_directory' );
复制代码



四、参考信息

filter:upload_dir是在wp_upload_dir()函数中调用的

$upload_dir = wp_upload_dir();

代码如下:

  1. $upload_dir now contains something like the following (if successful)
  2. Array (
  3. [path] => C:\path\to\wordpress\wp-content\uploads\2010\05
  4. [url] => <a href="http://example.com/wp-content/uploads/2010/05" target="_blank">http://example.com/wp-content/uploads/2010/05</a>
  5. [subdir] => /2010/05
  6. [basedir] => C:\path\to\wordpress\wp-content\uploads
  7. [baseurl] => <a href="http://example.com/wp-content/uploads" target="_blank">http://example.com/wp-content/uploads</a>
  8. [error] =>
  9. )
复制代码

 楼主| 易西 发表于 2023-10-16 20:14:00 | 显示全部楼层
 楼主| 易西 发表于 2023-10-16 20:15:39 | 显示全部楼层
https://joomlaseo.cn/blog-news/9 ... nge-wp-uploads.html
参考

一般我们默认WordPress上传文件路径是wp-content/uploads,但是有些网友考虑到路径太深,希望修改默认路径或者实现自定义文件路径。

比如我们有些网友希望在网站根目录设定一个images文件夹,然后所有的图片或者其他附件都上传到这个目录中。WordPress后台是没有办法实现的,我们需要修改配置代码是可以实现的。

  1. if(get_option('upload_path')=='wp-content/uploads' || get_option('upload_path')==null) {
  2. update_option('upload_path',WP_CONTENT_DIR.'/uploads');
  3. }
复制代码

这里我们可以在Functions.php添加文件,然后设置默认需要修改的路径。

然后我们可以看到在设置媒体中多一项,我们可以在这里自定义修改路径,也可以修改成本地绑定子目录地址独立二级域名作为图片的存储地址。
 楼主| 易西 发表于 2023-10-16 22:45:16 | 显示全部楼层
https://www.xiazaiqun.com/75685.html
参考

只需要在该文件中添加以下代码并保存更新该文件即可:

  1. define(\’UPLOADS\’,\’\’.\’/tupian\’);
复制代码


说明:/tupian 的意思就是以后图片存储目录就为根目录中的 tupian,可修改为其他名字。

添加代码后的 wp-config.php 文件:
[发帖际遇]: 一个袋子砸在了 易西 头上,易西 赚了 5 金钱. 幸运榜 / 衰神榜
 楼主| 易西 发表于 2023-10-16 23:59:49 | 显示全部楼层
https://zhidao.baidu.com/question/1776054971562485900.html
参考

1、登陆phpMyAdmin,选中你的WordPress数据库;

2、找到wp-options这个数据表;

3、打开这个表,找到第一条记录,也就是option_name为siteurl的那条记录,将option_value中错误的地址改为正确的即可。(如果option_name为home的那条记录地址也是错误的,改为正确的即可)

(1)WordPress是使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站。也可以把 WordPress当作一个内容管理系统(CMS)来使用。

(2)WordPress是一款个人博客系统,并逐步演化成一款内容管理系统软件,它是使用PHP语言和MySQL数据库开发的。用户可以在支持 PHP 和 MySQL数据库的服务器上使用自己的博客。

(3)WordPress有许多第三方开发的免费模板,安装方式简单易用。不过要做一个自己的模板,则需要你有一定的专业知识。比如你至少要懂的标准通用标记语言下的一个应用HTML代码、CSS、PHP等相关知识。

(4)WordPress官方支持中文版,同时有爱好者开发的第三方中文语言包,如wopus中文语言包。WordPress拥有成千上万个各式插件和不计其数的主题模板样式。
 楼主| 易西 发表于 2023-10-17 15:11:13 | 显示全部楼层
http://www.52itvip.com/1053.html
参考

在本文中,找到一个可以修改WordPress上传的文件目录,但只能在当前的网站根目录中实现,例如,在网站根目录images创建的文件夹或其他名称可以实现http://www.itbulu.com/images/这样的地址,或者如果我们是VPS和服务器,也可以绑定img.itbulu.com这种特定的网站格式。

第一、激活修改目录功能

  1. if(get_option(’;upload_path’;)==’;wp-content/uploads’;*||get_option(’;upload_path’;)==null)
  2. update_option(’;upload_path’;WP_CONTENT_DIR’;/uploads’;);
  3. }
复制代码

目前,新版本的WordPress后台媒体菜单无法修改目录。有人说以前是可以的。我们需要将上述脚本添加到当前主题的Functions.php文件中,然后启动激活。

二、修改上传文件夹目录

自动草稿
默认上传路径:此创建的文件夹目录需要在当前网站的根目录中

完整的URL地址:如果不单独设置二级域名或者绑定其他域名,可以直接留空。如果需要设置二级域名或者其他域名,需要将该域名分析到当前网站的IP地址,以及VPS和服务器配置上的目录。

最终,这样就可以实现WordPress上传图片文件夹的修改。但缺点是只能在本地,没有实现图片的远程上传和保存,大概看了一下好像还没有这样的教程,如果有朋友知道可以通知方法。
 楼主| 易西 发表于 2023-10-17 15:21:55 | 显示全部楼层
本帖最后由 易西 于 2023-10-17 15:25 编辑

http://www.thefox.cn/can-wordpress-modify-the-default-s.shtml
参考

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地址”选项,可以通过下面的代码将该选项调出来。
WordPress如何修改附件上传默认的存放目录?
将下面的代码添加到当前主题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

[发帖际遇]: 一个袋子砸在了 易西 头上,易西 赚了 3 金钱. 幸运榜 / 衰神榜
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

邮箱|首页|小黑屋|吾侪 ( 蜀ICP备2020029307号-4 )

GMT+8, 2026-9-17 19:11 , Processed in 0.037060 second(s), 25 queries .

Powered by Discuz! X3.5

Copyright © , 吾侪网

快速回复 返回顶部 返回列表