get_temp_dir(): string
- Since
- 2.5.0
- Source
wp-includes/functions.php:2222
Description
Function's preference is the return value of sys_get_temp_dir(), followed by your PHP temporary upload directory, followed by WP_CONTENT_DIR, before finally defaulting to /tmp/
In the event that this function does not find a writable location, It may be overridden by the WP_TEMP_DIR constant in your wp-config.php file.
Compatibility
- WordPress
- since 2.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Return value
string- Writable temporary directory.
Performance profile
How much work a call to get_temp_dir() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 8–40
- Plugin surface
- None
- Called by
- 5
Reads or writes the filesystem via is_dir().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 58.
Nothing here hands control to plugin code.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- filesystemfilesystem access
is_dir()called directly
What one call costs · 12 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_temp_dir() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8 | trailingslashit() |
!defined('WP_TEMP_DIR') && is_dir() && wp_is_writable() | 21 | sys_get_temp_dir(), is_dir(), wp_is_writable(), trailingslashit() |
!defined('WP_TEMP_DIR') && !is_dir() | 28 | sys_get_temp_dir(), is_dir(), is_dir(), is_dir() |
!defined('WP_TEMP_DIR') && wp_is_writable() | 28 | sys_get_temp_dir(), is_dir(), is_dir(), wp_is_writable(), trailingslashit() |
!defined('WP_TEMP_DIR') | 32 | sys_get_temp_dir(), is_dir(), is_dir(), is_dir(), wp_is_writable() |
!defined('WP_TEMP_DIR') && !wp_is_writable() | 32 | sys_get_temp_dir(), is_dir(), is_dir(), wp_is_writable(), is_dir() |
!defined('WP_TEMP_DIR') && !wp_is_writable() | 32 | sys_get_temp_dir(), is_dir(), wp_is_writable(), is_dir(), is_dir() |
!defined('WP_TEMP_DIR') && is_dir() | 32 | sys_get_temp_dir(), is_dir(), wp_is_writable(), is_dir(), wp_is_writable(), trailingslashit() |
!defined('WP_TEMP_DIR') && !wp_is_writable() | 36 | sys_get_temp_dir(), is_dir(), is_dir(), wp_is_writable(), is_dir(), wp_is_writable() |
!defined('WP_TEMP_DIR') && !wp_is_writable() | 36 | sys_get_temp_dir(), is_dir(), wp_is_writable(), is_dir(), is_dir(), wp_is_writable() |
!defined('WP_TEMP_DIR') && !wp_is_writable() | 36 | sys_get_temp_dir(), is_dir(), wp_is_writable(), is_dir(), wp_is_writable(), is_dir() |
!defined('WP_TEMP_DIR') && is_dir() && !wp_is_writable() | 40 | sys_get_temp_dir(), is_dir(), wp_is_writable(), is_dir(), wp_is_writable(), is_dir(), wp_is_writable() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 58 instructions, 8–40 executed per call, 8 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 2
- trailingslashit()Appends a trailing slash.
- wp_is_writable()Determines if a directory is writable.
Used by · 5
- WP_Http::request()Send an HTTP request to a URI.
- wp_generate_block_templates_export_file()Creates an export of the current templates and template parts from the site editor at the specified path in a ZIP file.
- wp_read_audio_metadata()Retrieves metadata from an audio file's ID3 tags.
- wp_read_video_metadata()Retrieves metadata from a video file's ID3 tags.
- wp_tempnam()Returns a filename of a temporary unique file.
Source code
function get_temp_dir() { static $temp = ''; if ( defined( 'WP_TEMP_DIR' ) ) { return trailingslashit( WP_TEMP_DIR ); } if ( $temp ) { return trailingslashit( $temp ); } if ( function_exists( 'sys_get_temp_dir' ) ) { $temp = sys_get_temp_dir(); if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) { return trailingslashit( $temp ); } } $temp = ini_get( 'upload_tmp_dir' ); if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) { return trailingslashit( $temp ); } $temp = WP_CONTENT_DIR . '/'; if ( is_dir( $temp ) && wp_is_writable( $temp ) ) { return $temp; } return '/tmp/';}Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.