wp_get_attachment_url()WP 2.1.0

Retrieve the URL for an attachment.

1 time — 0.0009661 sec (slow) | 50000 times — 0.28 sec (very fast) | PHP 7.3.12, WP 5.3.2
Hooks from the function

Return

String|false. Attachment URL, otherwise false.

Usage

wp_get_attachment_url( $attachment_id );
$attachment_id(int)
Attachment post ID.
Default: global $post

Examples

0

#1 Get the link to the attachment

Let's say we added a picture to the media library and it has an ID 55, then we can get a link to that picture this way:

$image_url = wp_get_attachment_url( 55 );
echo $image_url;

// Returns: http://example.com/wp-content/uploads/image.png
0

#2 Display the image attached to the post

Let's say we want to display an image attached to post 5. Then first we need to find out the ID of the attachment and then display the image:

// Get the ID of the attachment of post 5
$id = 5;
$attachment_image = get_children( array(
	'numberposts' => 1,
	'post_mime_type' => 'image',
	'post_parent' => $id,
	'post_type' => 'attachment'
) );

// take the first picture out of the array
$attachment_image = array_shift( $attachment_image );

$img = '<img src="' . wp_get_attachment_url( $attachment_image->ID ) . '" alt="" />';

echo $img;

// will return:
// <img src="http://example.com/wp-content/uploads/2011/07/robots.txt.jpg" alt="" />
0

#3 Post's thumbnail as a 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>';
		}
	}
}

Notes

  • Global. String. $pagenow The filename of the current screen.

Changelog

Since 2.1.0 Introduced.

Code of wp_get_attachment_url() WP 6.0.1

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