get_temp_dir

The timeline below displays how wordpress function get_temp_dir has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Determines a writable directory for temporary files.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @return string Writable temporary directory.
 */
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/';
}

WordPress Version: 5.5

/**
 * Determine a writable directory for temporary files.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @return string Writable temporary directory.
 */
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/';
}

WordPress Version: 4.3

/**
 * Determine a writable directory for temporary files.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @staticvar string $temp
 *
 * @return string Writable temporary directory.
 */
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/';
}

WordPress Version: 4.0

/**
 * Determine a writable directory for temporary files.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @return string Writable temporary directory.
 */
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;
    }
    $temp = '/tmp/';
    return $temp;
}

WordPress Version: 3.9

/**
 * Determines a writable directory for temporary files.
 * Function's preference is the return value of <code>sys_get_temp_dir()</code>,
 * 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 <code>WP_TEMP_DIR</code> constant in
 * your <code>wp-config.php</code> file.
 *
 * @since 2.5.0
 *
 * @return string Writable temporary directory
 */
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;
    }
    $temp = '/tmp/';
    return $temp;
}

WordPress Version: 3.7

/**
 * Determines a writable directory for temporary files.
 * Function's preference is the return value of <code>sys_get_temp_dir()</code>,
 * 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 <code>WP_TEMP_DIR</code> constant in
 * your <code>wp-config.php</code> file.
 *
 * @since 2.5.0
 *
 * @return string Writable temporary directory
 */
function get_temp_dir()
{
    static $temp;
    if (defined('WP_TEMP_DIR')) {
        return trailingslashit(WP_TEMP_DIR);
    }
    if ($temp) {
        return trailingslashit(rtrim($temp, '\\'));
    }
    if (function_exists('sys_get_temp_dir')) {
        $temp = sys_get_temp_dir();
        if (@is_dir($temp) && wp_is_writable($temp)) {
            return trailingslashit(rtrim($temp, '\\'));
        }
    }
    $temp = ini_get('upload_tmp_dir');
    if (@is_dir($temp) && wp_is_writable($temp)) {
        return trailingslashit(rtrim($temp, '\\'));
    }
    $temp = WP_CONTENT_DIR . '/';
    if (is_dir($temp) && wp_is_writable($temp)) {
        return $temp;
    }
    $temp = '/tmp/';
    return $temp;
}