wp_get_attachment_url

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

WordPress Version: 6.3

/**
 * Retrieves the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @global string $pagenow The filename of the current screen.
 *
 * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
 * @return string|false Attachment URL, otherwise false.
 */
function wp_get_attachment_url($attachment_id = 0)
{
    global $pagenow;
    $attachment_id = (int) $attachment_id;
    $post = get_post($attachment_id);
    if (!$post) {
        return false;
    }
    if ('attachment' !== $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    $file = get_post_meta($post->ID, '_wp_attached_file', true);
    if ($file) {
        // Get upload directory.
        $uploads = wp_get_upload_dir();
        if ($uploads && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (str_starts_with($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (str_contains($file, 'wp-content/uploads')) {
                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
                $url = trailingslashit($uploads['baseurl'] . '/' . _wp_get_attachment_relative_path($file)) . wp_basename($file);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (!$url) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $pagenow) {
        $url = set_url_scheme($url);
    }
    /**
     * Filters the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url           URL for the given attachment.
     * @param int    $attachment_id Attachment post ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (!$url) {
        return false;
    }
    return $url;
}

WordPress Version: 6.1

/**
 * Retrieves the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @global string $pagenow The filename of the current screen.
 *
 * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
 * @return string|false Attachment URL, otherwise false.
 */
function wp_get_attachment_url($attachment_id = 0)
{
    global $pagenow;
    $attachment_id = (int) $attachment_id;
    $post = get_post($attachment_id);
    if (!$post) {
        return false;
    }
    if ('attachment' !== $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    $file = get_post_meta($post->ID, '_wp_attached_file', true);
    if ($file) {
        // Get upload directory.
        $uploads = wp_get_upload_dir();
        if ($uploads && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
                $url = trailingslashit($uploads['baseurl'] . '/' . _wp_get_attachment_relative_path($file)) . wp_basename($file);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (!$url) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $pagenow) {
        $url = set_url_scheme($url);
    }
    /**
     * Filters the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url           URL for the given attachment.
     * @param int    $attachment_id Attachment post ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (!$url) {
        return false;
    }
    return $url;
}

WordPress Version: 5.7

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @global string $pagenow
 *
 * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
 * @return string|false Attachment URL, otherwise false.
 */
function wp_get_attachment_url($attachment_id = 0)
{
    global $pagenow;
    $attachment_id = (int) $attachment_id;
    $post = get_post($attachment_id);
    if (!$post) {
        return false;
    }
    if ('attachment' !== $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    $file = get_post_meta($post->ID, '_wp_attached_file', true);
    if ($file) {
        // Get upload directory.
        $uploads = wp_get_upload_dir();
        if ($uploads && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
                $url = trailingslashit($uploads['baseurl'] . '/' . _wp_get_attachment_relative_path($file)) . wp_basename($file);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (!$url) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $pagenow) {
        $url = set_url_scheme($url);
    }
    /**
     * Filters the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url           URL for the given attachment.
     * @param int    $attachment_id Attachment post ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (!$url) {
        return false;
    }
    return $url;
}

WordPress Version: 5.6

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @global string $pagenow
 *
 * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
 * @return string|false Attachment URL, otherwise false.
 */
function wp_get_attachment_url($attachment_id = 0)
{
    $attachment_id = (int) $attachment_id;
    $post = get_post($attachment_id);
    if (!$post) {
        return false;
    }
    if ('attachment' !== $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    $file = get_post_meta($post->ID, '_wp_attached_file', true);
    if ($file) {
        // Get upload directory.
        $uploads = wp_get_upload_dir();
        if ($uploads && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
                $url = trailingslashit($uploads['baseurl'] . '/' . _wp_get_attachment_relative_path($file)) . wp_basename($file);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (!$url) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $GLOBALS['pagenow']) {
        $url = set_url_scheme($url);
    }
    /**
     * Filters the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url           URL for the given attachment.
     * @param int    $attachment_id Attachment post ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (!$url) {
        return false;
    }
    return $url;
}

WordPress Version: 5.5

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @global string $pagenow
 *
 * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
 * @return string|false Attachment URL, otherwise false.
 */
function wp_get_attachment_url($attachment_id = 0)
{
    $attachment_id = (int) $attachment_id;
    $post = get_post($attachment_id);
    if (!$post) {
        return false;
    }
    if ('attachment' !== $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    $file = get_post_meta($post->ID, '_wp_attached_file', true);
    if ($file) {
        // Get upload directory.
        $uploads = wp_get_upload_dir();
        if ($uploads && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
                $url = trailingslashit($uploads['baseurl'] . '/' . _wp_get_attachment_relative_path($file)) . wp_basename($file);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (empty($url)) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $GLOBALS['pagenow']) {
        $url = set_url_scheme($url);
    }
    /**
     * Filters the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url           URL for the given attachment.
     * @param int    $attachment_id Attachment post ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}

WordPress Version: 5.4

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @global string $pagenow
 *
 * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
 * @return string|false Attachment URL, otherwise false.
 */
function wp_get_attachment_url($attachment_id = 0)
{
    $attachment_id = (int) $attachment_id;
    $post = get_post($attachment_id);
    if (!$post) {
        return false;
    }
    if ('attachment' != $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    $file = get_post_meta($post->ID, '_wp_attached_file', true);
    if ($file) {
        // Get upload directory.
        $uploads = wp_get_upload_dir();
        if ($uploads && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
                $url = trailingslashit($uploads['baseurl'] . '/' . _wp_get_attachment_relative_path($file)) . wp_basename($file);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (empty($url)) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $GLOBALS['pagenow']) {
        $url = set_url_scheme($url);
    }
    /**
     * Filters the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url           URL for the given attachment.
     * @param int    $attachment_id Attachment post ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}

WordPress Version: 5.3

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @global string $pagenow
 *
 * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
 * @return string|false Attachment URL, otherwise false.
 */
function wp_get_attachment_url($attachment_id = 0)
{
    $attachment_id = (int) $attachment_id;
    $post = get_post($attachment_id);
    if (!$post) {
        return false;
    }
    if ('attachment' != $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    $file = get_post_meta($post->ID, '_wp_attached_file', true);
    if ($file) {
        // Get upload directory.
        $uploads = wp_get_upload_dir();
        if ($uploads && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads)
                $url = trailingslashit($uploads['baseurl'] . '/' . _wp_get_attachment_relative_path($file)) . wp_basename($file);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (empty($url)) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $GLOBALS['pagenow']) {
        $url = set_url_scheme($url);
    }
    /**
     * Filters the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url           URL for the given attachment.
     * @param int    $attachment_id Attachment post ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}

WordPress Version: 5.2

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @global string $pagenow
 *
 * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
 * @return string|false Attachment URL, otherwise false.
 */
function wp_get_attachment_url($attachment_id = 0)
{
    $attachment_id = (int) $attachment_id;
    if (!$post = get_post($attachment_id)) {
        return false;
    }
    if ('attachment' != $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    if ($file = get_post_meta($post->ID, '_wp_attached_file', true)) {
        // Get upload directory.
        if (($uploads = wp_get_upload_dir()) && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads)
                $url = trailingslashit($uploads['baseurl'] . '/' . _wp_get_attachment_relative_path($file)) . wp_basename($file);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (empty($url)) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $GLOBALS['pagenow']) {
        $url = set_url_scheme($url);
    }
    /**
     * Filters the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url           URL for the given attachment.
     * @param int    $attachment_id Attachment post ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}

WordPress Version: 4.9

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @global string $pagenow
 *
 * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
 * @return string|false Attachment URL, otherwise false.
 */
function wp_get_attachment_url($attachment_id = 0)
{
    $attachment_id = (int) $attachment_id;
    if (!$post = get_post($attachment_id)) {
        return false;
    }
    if ('attachment' != $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    if ($file = get_post_meta($post->ID, '_wp_attached_file', true)) {
        // Get upload directory.
        if (($uploads = wp_get_upload_dir()) && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads)
                $url = trailingslashit($uploads['baseurl'] . '/' . _wp_get_attachment_relative_path($file)) . basename($file);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (empty($url)) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $GLOBALS['pagenow']) {
        $url = set_url_scheme($url);
    }
    /**
     * Filters the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url           URL for the given attachment.
     * @param int    $attachment_id Attachment post ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}

WordPress Version: 4.6

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @global string $pagenow
 *
 * @param int $post_id Optional. Attachment ID. Default 0.
 * @return string|false Attachment URL, otherwise false.
 */
function wp_get_attachment_url($post_id = 0)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    if ('attachment' != $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    if ($file = get_post_meta($post->ID, '_wp_attached_file', true)) {
        // Get upload directory.
        if (($uploads = wp_get_upload_dir()) && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads)
                $url = trailingslashit($uploads['baseurl'] . '/' . _wp_get_attachment_relative_path($file)) . basename($file);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (empty($url)) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $GLOBALS['pagenow']) {
        $url = set_url_scheme($url);
    }
    /**
     * Filters the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url     URL for the given attachment.
     * @param int    $post_id Attachment ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}

WordPress Version: 4.5

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @global string $pagenow
 *
 * @param int $post_id Optional. Attachment ID. Default 0.
 * @return string|false Attachment URL, otherwise false.
 */
function wp_get_attachment_url($post_id = 0)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    if ('attachment' != $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    if ($file = get_post_meta($post->ID, '_wp_attached_file', true)) {
        // Get upload directory.
        if (($uploads = wp_get_upload_dir()) && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads)
                $url = trailingslashit($uploads['baseurl'] . '/' . _wp_get_attachment_relative_path($file)) . basename($file);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (empty($url)) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $GLOBALS['pagenow']) {
        $url = set_url_scheme($url);
    }
    /**
     * Filter the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url     URL for the given attachment.
     * @param int    $post_id Attachment ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}

WordPress Version: 4.1

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @global string $pagenow
 *
 * @param int $post_id Optional. Attachment ID. Default 0.
 * @return string|false Attachment URL, otherwise false.
 */
function wp_get_attachment_url($post_id = 0)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    if ('attachment' != $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    if ($file = get_post_meta($post->ID, '_wp_attached_file', true)) {
        // Get upload directory.
        if (($uploads = wp_upload_dir()) && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads)
                $url = trailingslashit($uploads['baseurl'] . '/' . _wp_get_attachment_relative_path($file)) . basename($file);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (empty($url)) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front-end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $GLOBALS['pagenow']) {
        $url = set_url_scheme($url);
    }
    /**
     * Filter the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url     URL for the given attachment.
     * @param int    $post_id Attachment ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}

WordPress Version: 4.3

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @global string $pagenow
 *
 * @param int $post_id Optional. Attachment ID. Default 0.
 * @return string|false Attachment URL, otherwise false.
 */
function wp_get_attachment_url($post_id = 0)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    if ('attachment' != $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    if ($file = get_post_meta($post->ID, '_wp_attached_file', true)) {
        // Get upload directory.
        if (($uploads = wp_upload_dir()) && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                $url = $uploads['baseurl'] . substr($file, strpos($file, 'wp-content/uploads') + 18);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (empty($url)) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front-end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $GLOBALS['pagenow']) {
        $url = set_url_scheme($url);
    }
    /**
     * Filter the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url     URL for the given attachment.
     * @param int    $post_id Attachment ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}

WordPress Version: .10

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @param int $post_id Optional. Attachment ID. Default 0.
 * @return string|bool Attachment URL, otherwise false.
 */
function wp_get_attachment_url($post_id = 0)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    if ('attachment' != $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    if ($file = get_post_meta($post->ID, '_wp_attached_file', true)) {
        // Get upload directory.
        if (($uploads = wp_upload_dir()) && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                $url = $uploads['baseurl'] . substr($file, strpos($file, 'wp-content/uploads') + 18);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (empty($url)) {
        $url = get_the_guid($post->ID);
    }
    // On SSL front-end, URLs should be HTTPS.
    if (is_ssl() && !is_admin() && 'wp-login.php' !== $GLOBALS['pagenow']) {
        $url = set_url_scheme($url);
    }
    /**
     * Filter the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url     URL for the given attachment.
     * @param int    $post_id Attachment ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}

WordPress Version: 4.2

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @param int $post_id Optional. Attachment ID. Default 0.
 * @return string|bool Attachment URL, otherwise false.
 */
function wp_get_attachment_url($post_id = 0)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    if ('attachment' != $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    if ($file = get_post_meta($post->ID, '_wp_attached_file', true)) {
        // Get upload directory.
        if (($uploads = wp_upload_dir()) && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                $url = $uploads['baseurl'] . substr($file, strpos($file, 'wp-content/uploads') + 18);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (empty($url)) {
        $url = get_the_guid($post->ID);
    }
    /*
     * If currently on SSL, prefer HTTPS URLs when we know they're supported by the domain
     * (which is to say, when they share the domain name of the current SSL page).
     */
    if (is_ssl() && 'https' !== substr($url, 0, 5) && parse_url($url, PHP_URL_HOST) === $_SERVER['HTTP_HOST']) {
        $url = set_url_scheme($url, 'https');
    }
    /**
     * Filter the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url     URL for the given attachment.
     * @param int    $post_id Attachment ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}

WordPress Version: 4.0

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @param int $post_id Optional. Attachment ID. Default 0.
 * @return string|bool Attachment URL, otherwise false.
 */
function wp_get_attachment_url($post_id = 0)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    if ('attachment' != $post->post_type) {
        return false;
    }
    $url = '';
    // Get attached file.
    if ($file = get_post_meta($post->ID, '_wp_attached_file', true)) {
        // Get upload directory.
        if (($uploads = wp_upload_dir()) && false === $uploads['error']) {
            // Check that the upload base exists in the file location.
            if (0 === strpos($file, $uploads['basedir'])) {
                // Replace file location with url location.
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                $url = $uploads['baseurl'] . substr($file, strpos($file, 'wp-content/uploads') + 18);
            } else {
                // It's a newly-uploaded file, therefore $file is relative to the basedir.
                $url = $uploads['baseurl'] . "/{$file}";
            }
        }
    }
    /*
     * If any of the above options failed, Fallback on the GUID as used pre-2.7,
     * not recommended to rely upon this.
     */
    if (empty($url)) {
        $url = get_the_guid($post->ID);
    }
    /**
     * Filter the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url     URL for the given attachment.
     * @param int    $post_id Attachment ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}

WordPress Version: 3.9

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @param int $post_id Attachment ID.
 * @return string
 */
function wp_get_attachment_url($post_id = 0)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    if ('attachment' != $post->post_type) {
        return false;
    }
    $url = '';
    if ($file = get_post_meta($post->ID, '_wp_attached_file', true)) {
        //Get attached file
        if (($uploads = wp_upload_dir()) && false === $uploads['error']) {
            //Get upload directory
            if (0 === strpos($file, $uploads['basedir'])) {
                //Check that the upload base exists in the file location
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                $url = $uploads['baseurl'] . substr($file, strpos($file, 'wp-content/uploads') + 18);
            } else {
                $url = $uploads['baseurl'] . "/{$file}";
            }
            //Its a newly uploaded file, therefor $file is relative to the basedir.
        }
    }
    if (empty($url)) {
        //If any of the above options failed, Fallback on the GUID as used pre-2.7, not recommended to rely upon this.
        $url = get_the_guid($post->ID);
    }
    /**
     * Filter the attachment URL.
     *
     * @since 2.1.0
     *
     * @param string $url     URL for the given attachment.
     * @param int    $post_id Attachment ID.
     */
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}

WordPress Version: 3.7

/**
 * Retrieve the URL for an attachment.
 *
 * @since 2.1.0
 *
 * @param int $post_id Attachment ID.
 * @return string
 */
function wp_get_attachment_url($post_id = 0)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    if ('attachment' != $post->post_type) {
        return false;
    }
    $url = '';
    if ($file = get_post_meta($post->ID, '_wp_attached_file', true)) {
        //Get attached file
        if (($uploads = wp_upload_dir()) && false === $uploads['error']) {
            //Get upload directory
            if (0 === strpos($file, $uploads['basedir'])) {
                //Check that the upload base exists in the file location
                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
            } elseif (false !== strpos($file, 'wp-content/uploads')) {
                $url = $uploads['baseurl'] . substr($file, strpos($file, 'wp-content/uploads') + 18);
            } else {
                $url = $uploads['baseurl'] . "/{$file}";
            }
            //Its a newly uploaded file, therefor $file is relative to the basedir.
        }
    }
    if (empty($url)) {
        //If any of the above options failed, Fallback on the GUID as used pre-2.7, not recommended to rely upon this.
        $url = get_the_guid($post->ID);
    }
    $url = apply_filters('wp_get_attachment_url', $url, $post->ID);
    if (empty($url)) {
        return false;
    }
    return $url;
}