函数文档

wp_get_upload_dir()

💡 云策文档标注

概述

wp_get_upload_dir() 是一个轻量级函数,用于获取 WordPress 上传目录信息,类似于 wp_upload_dir(),但不会尝试创建上传目录。它适用于主题开发中,当仅需要 'basedir' 和 'baseurl' 时,特别是在不涉及文件上传的场景。

关键要点

  • 函数返回一个数组,包含上传目录的路径和 URL 等信息,具体结构可参考 wp_upload_dir()。
  • 它是 wp_upload_dir() 的轻量版本,通过调用 wp_upload_dir(null, false) 实现,避免了目录创建操作。
  • 主要用于主题开发,当仅需基于目录和基础 URL 时,提高性能。
  • 相关函数包括 wp_upload_dir()、wp_get_attachment_url() 等,用于处理附件和上传文件。
  • 自 WordPress 4.5.0 版本引入,无后续变更记录。

代码示例

array(6) {
    ["path"] => string(61) "server/wp-content/uploads/2019/07"
    ["url"] => string(46) "https://example.com/wp-content/uploads/2019/07"
    ["subdir"] => string(8) "/2019/07"
    ["basedir"] => string(53) "server/wp-content/uploads"
    ["baseurl"] => string(38) "https://example.com/wp-content/uploads"
    ["error"] => bool(false)
}

📄 原文内容

Retrieves uploads directory information.

Description

Same as wp_upload_dir() but “light weight” as it doesn’t attempt to create the uploads directory.
Intended for use in themes, when only ‘basedir’ and ‘baseurl’ are needed, generally in all cases when not uploading files.

See also

Return

array See wp_upload_dir() for description.

Source

function wp_get_upload_dir() {
	return wp_upload_dir( null, false );
}

Changelog

Version Description
4.5.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content


    Anonymous User



    Sample result of the function

    If it is called in index.php:

    array(6) {
    	["path"] => string(61) "server/wp-content/uploads/2019/07"
    	["url"] => string(46) "<a href="https://example.com/wp-content/uploads/2019/07&quot" rel="nofollow ugc">https://example.com/wp-content/uploads/2019/07"</a>;
    	["subdir"] => string(8) "/2019/07"
    	["basedir"] => string(53) "server/wp-content/uploads"
    	["baseurl"] => string(38) "<a href="https://example.com/wp-content/uploads&quot" rel="nofollow ugc">https://example.com/wp-content/uploads"</a>;
    	["error"] => bool(false)
    }