wordpress上传的文件在哪
wordpress上传的文件在哪?
WordPress默认的上传文件的目录是/wp-content/uploads,而且文件是以年月的形式组织的,虽然我们可以去掉按照年月组织的选项,或者更改存储路径,但这个设置会应用到全局,不能按照特定条件选择特定目录存储文件。
WordPress上传默认设置
有时候将不同类型的文件分门别类存储,似乎比年月目录更有意义。例如幻灯片应该存储在slides目录下,下载文件应该存储在downloads文件夹下。就说幻灯片slideshow,我比较喜欢用自定义文章类型(Custom Post Type)实现,有些幻灯片脚本比较个性,不支持绝对路径,必须用相对路径,然后用base参数设置相对于哪个文件夹,这样幻灯片必须存储在某个特定的文件夹中,年月形式显然不满足要求。所以,我们需要条件化的设置上传目录。
为Custom Post Type设置上传目录
假设我要将所有在幻灯片类型的文章中上传的文件存储到/wp-content/uploads/slides文件夹中,将下面的代码放到主题的functions.php中即可
function custom_upload_directory( $uploads ) { $id = $_REQUEST['post_id']; $parent = get_post( $id )->post_parent; if( "post-type" == get_post_type( $id ) || "post-type" == get_post_type( $parent ) ) { $subdir = 'slides'; $uploads['subdir'] = $subdir; $uploads['path'] = $uploads['basedir'].DIRECTORY_SEPARATOR.$subdir; $uploads['url'] = $uploads['baseurl'].'/'.$subdir; } return $uploads; } 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'];
这段代码来自 http://wordpress.stackexchange.com/questions/35657/how-to-add-more-upload-directories/
为后台管理页面设定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想等,就更该目录。
function custom_upload_directory( $uploads ) { if( wp_get_referer() == 'http://domain.com/wp-admin/admin.php?page=myoptionpage'){ $subdir = 'myfolder'; $uploads['subdir'] = $subdir; $uploads['path'] = $uploads['basedir'].DIRECTORY_SEPARATOR.$subdir; $uploads['url'] = $uploads['baseurl'].'/'.$subdir; } return $uploads; } add_filter( 'upload_dir', 'custom_upload_directory' );
参考信息
filter:upload_dir是在wp_upload_dir()函数中调用的
$upload_dir = wp_upload_dir(); $upload_dir now contains something like the following (if successful) Array ( [path] => C:\path\to\wordpress\wp-content\uploads\2010\05 [url] => http://example.com/wp-content/uploads/2010/05 [subdir] => /2010/05 [basedir] => C:\path\to\wordpress\wp-content\uploads [baseurl] => http://example.com/wp-content/uploads [error] => )
更多WordPress技术文章,请访问WordPress教程栏目!
以上是wordpress上传的文件在哪的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

有四种方法可以调整 WordPress 文章列表:使用主题选项、使用插件(如 Post Types Order、WP Post List、Boxy Stuff)、使用代码(在 functions.php 文件中添加设置)或直接修改 WordPress 数据库。

在 WordPress 中添加评论功能步骤如下:启用评论:勾选“允许评论”以启用特定文章或所有文章的评论。设置评论选项:自定义评论审批、分页、嵌套评论等设置。添加评论表单:使用提供的代码将评论表单添加到所需位置。显示评论:使用提供的代码显示已提交的评论。回复评论:通过“回复”链接回复单个评论,或使用批量操作回复多个评论。管理评论:在“评论”选项卡中查看、删除、批准和标记评论。

没有完美的WordPress产品注册插件,选择应根据实际需求和网站规模。推荐插件包括:MemberPress:功能强大但价格较高,配置复杂Restrict Content Pro:专注内容限制和会员管理,性价比高Easy Digital Downloads:销售数字产品,用户注册为附加功能

登录 WordPress 网站账号的步骤:访问登录页面:输入网站网址加上 "/wp-login.php"。输入用户名和密码。点击“登录”。验证两步验证(可选)。成功登录后,您将看到网站仪表盘。

WordPress 编辑日期可以通过三种方法取消:1. 安装 Enable Post Date Disable 插件;2. 在 functions.php 文件中添加代码;3. 手动编辑 wp_posts 表中的 post_modified 列。

您可以使用 WordPress 主题编辑器或寻求第三方帮助,例如预构建的主题、主题生成器或聘请开发人员,以创建或自定义自己的 WordPress 主题。通过了解主题基础知识、使用子主题、测试更改和优化性能,您可以打造一个独特且有效的在线形象。

WordPress 错误解决指南:500 内部服务器错误:禁用插件或检查服务器错误日志。404 未找到页面:检查 permalink 并确保页面链接正确。白屏死机:增加服务器 PHP 内存限制。数据库连接错误:检查数据库服务器状态和 WordPress 配置。其他技巧:启用调试模式、检查错误日志和寻求支持。预防错误:定期更新 WordPress、仅安装必要插件、定期备份网站和优化网站性能。

如何复制 WordPress 代码?从管理界面复制:登录 WordPress 网站,导航到目标位置,选择代码并按 Ctrl C (Windows)/Command C (Mac) 复制代码。从文件复制:使用 SSH 或 FTP 连接到服务器,导航到主题或插件文件,选择代码并按 Ctrl C (Windows)/Command C (Mac) 复制代码。
