wp_get_attachment_url( int $attachment_id )

Retrieve the URL for an attachment.


Parameters Parameters

$attachment_id

(int) (Optional) Attachment post ID. Defaults to global $post.


Top ↑

Return Return

(string|false) Attachment URL, otherwise false.


Top ↑

More Information More Information

You can change the output of this function through the wp get attachment url filter.

This function will not URL encode the URL. If you have attachments with invalid characters in their name, you should raw URL encode the output of this function in order to have a valid URL.

Sample code that gives you a root-relative URL to your attachment:

<pre>$parsed = parse_url( wp_get_attachment_url( $attachment->ID ) );
$url = dirname( $parsed [ 'path' ] ) . '/' . rawurlencode( basename( $parsed[ 'path' ] ) );</pre>
<pre>

If you want a URI for the attachment page, not the attachment file itself, you can use get_attachment_link.

Also refer: wp_insert_attachmentwp_upload_dirwp_get_attachment_image_src


Top ↑

Source Source

File: wp-includes/post.php

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;
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Use Post Thumbnail as Background Image

    if ( have_posts() ) : while ( have_posts() ) : the_post(); 
        if ( has_post_thumbnail() ) {
            $feat_image_url = wp_get_attachment_url( get_post_thumbnail_id() );
            echo '<div style="background-image:url('.$feat_image_url.');"></div>';
        }
        endwhile;
    endif;
    

You must log in before being able to contribute a note or feedback.