Title: wp_get_attachment_url
Published: April 25, 2014
Last modified: May 20, 2026

---

# wp_get_attachment_url( int $attachment_id ): string|false

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#wp--skip-link--target)

Retrieves the URL for an attachment.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#parameters)󠁿

 `$attachment_id`intoptional

Attachment post ID. Defaults to global $post.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#return)󠁿

 string|false Attachment URL, otherwise false.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#more-information)󠁿

You can change the output of this function through the [wp get attachment url](https://developer.wordpress.org/reference/hooks/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:

    ```php
    <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](https://codex.wordpress.org/Templates_Hierarchy#Attachment_display),
not the attachment file itself, you can use [get_attachment_link](https://developer.wordpress.org/reference/functions/get_attachment_link/).

Also refer: [wp_insert_attachment](https://developer.wordpress.org/reference/functions/wp_insert_attachment/),
[wp_upload_dir](https://developer.wordpress.org/reference/functions/wp_upload_dir/),
[wp_get_attachment_image_src](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/)

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#source)󠁿

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

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/post.php#L6969)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/post.php#L6969-L7033)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#hooks)󠁿

 [apply_filters( ‘wp_get_attachment_url’, string $url, int $attachment_id )](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/)

Filters the attachment URL.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_get_upload_dir()](https://developer.wordpress.org/reference/functions/wp_get_upload_dir/)`wp-includes/functions.php` |

Retrieves uploads directory information.

  | 
| [_wp_get_attachment_relative_path()](https://developer.wordpress.org/reference/functions/_wp_get_attachment_relative_path/)`wp-includes/media.php` |

Gets the attachment path relative to the upload directory.

  | 
| [is_ssl()](https://developer.wordpress.org/reference/functions/is_ssl/)`wp-includes/load.php` |

Determines if SSL is used.

  | 
| [set_url_scheme()](https://developer.wordpress.org/reference/functions/set_url_scheme/)`wp-includes/link-template.php` |

Sets the scheme for a URL.

  | 
| [get_the_guid()](https://developer.wordpress.org/reference/functions/get_the_guid/)`wp-includes/post-template.php` |

Retrieves the Post Global Unique Identifier (guid).

  | 
| [wp_basename()](https://developer.wordpress.org/reference/functions/wp_basename/)`wp-includes/formatting.php` |

i18n-friendly version of basename().

  | 
| [trailingslashit()](https://developer.wordpress.org/reference/functions/trailingslashit/)`wp-includes/formatting.php` |

Appends a trailing slash.

  | 
| [is_admin()](https://developer.wordpress.org/reference/functions/is_admin/)`wp-includes/load.php` |

Determines whether the current request is for an administrative interface page.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [get_post_meta()](https://developer.wordpress.org/reference/functions/get_post_meta/)`wp-includes/post.php` |

Retrieves a post meta field for the given post ID.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  |

[Show 6 more](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#)

| Used by | Description | 
| [wp_copy_parent_attachment_properties()](https://developer.wordpress.org/reference/functions/wp_copy_parent_attachment_properties/)`wp-admin/includes/image.php` |

Copy parent attachment properties to newly cropped image.

  | 
| [get_media_states()](https://developer.wordpress.org/reference/functions/get_media_states/)`wp-admin/includes/template.php` |

Retrieves an array of media states from an attachment.

  | 
| [wp_get_original_image_url()](https://developer.wordpress.org/reference/functions/wp_get_original_image_url/)`wp-includes/post.php` |

Retrieves the URL to an original attachment image.

  | 
| [wp_media_personal_data_exporter()](https://developer.wordpress.org/reference/functions/wp_media_personal_data_exporter/)`wp-includes/media.php` |

Finds and exports attachments associated with an email address.

  | 
| [WP_Widget_Media_Audio::render_media()](https://developer.wordpress.org/reference/classes/wp_widget_media_audio/render_media/)`wp-includes/widgets/class-wp-widget-media-audio.php` |

Render the media on the frontend.

  | 
| [WP_Widget_Media_Video::render_media()](https://developer.wordpress.org/reference/classes/wp_widget_media_video/render_media/)`wp-includes/widgets/class-wp-widget-media-video.php` |

Render the media on the frontend.

  | 
| [WP_Widget_Media_Image::render_media()](https://developer.wordpress.org/reference/classes/wp_widget_media_image/render_media/)`wp-includes/widgets/class-wp-widget-media-image.php` |

Render the media on the frontend.

  | 
| [WP_Customize_Manager::import_theme_starter_content()](https://developer.wordpress.org/reference/classes/wp_customize_manager/import_theme_starter_content/)`wp-includes/class-wp-customize-manager.php` |

Imports theme starter content into the customized state.

  | 
| [get_header_video_url()](https://developer.wordpress.org/reference/functions/get_header_video_url/)`wp-includes/theme.php` |

Retrieves header video URL for custom header.

  | 
| [WP_REST_Attachments_Controller::prepare_item_for_response()](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/prepare_item_for_response/)`wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php` |

Prepares a single attachment output for response.

  | 
| [WP_Site_Icon::create_attachment_object()](https://developer.wordpress.org/reference/classes/wp_site_icon/create_attachment_object/)`wp-admin/includes/class-wp-site-icon.php` |

Creates an attachment ‘object’.

  | 
| [export_wp()](https://developer.wordpress.org/reference/functions/export_wp/)`wp-admin/includes/export.php` |

Generates the WXR export file for download.

  | 
| [wp_save_image()](https://developer.wordpress.org/reference/functions/wp_save_image/)`wp-admin/includes/image-edit.php` |

Saves image to post, along with enqueued changes in `$_REQUEST['history']`.

  | 
| [_load_image_to_edit_path()](https://developer.wordpress.org/reference/functions/_load_image_to_edit_path/)`wp-admin/includes/image.php` |

Retrieves the path or URL of an attachment’s attached file.

  | 
| [edit_form_image_editor()](https://developer.wordpress.org/reference/functions/edit_form_image_editor/)`wp-admin/includes/media.php` |

Displays the image and editor in the post editor

  | 
| [attachment_submitbox_metadata()](https://developer.wordpress.org/reference/functions/attachment_submitbox_metadata/)`wp-admin/includes/media.php` |

Displays non-editable attachment metadata in the publish meta box.

  | 
| [get_attachment_fields_to_edit()](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/)`wp-admin/includes/media.php` |

Retrieves the attachment fields to edit form fields.

  | 
| [media_sideload_image()](https://developer.wordpress.org/reference/functions/media_sideload_image/)`wp-admin/includes/media.php` |

Downloads an image from the specified URL, saves it as an attachment, and optionally attaches it to a post.

  | 
| [image_link_input_fields()](https://developer.wordpress.org/reference/functions/image_link_input_fields/)`wp-admin/includes/media.php` |

Retrieves HTML for the Link URL buttons with the default link type as specified.

  | 
| [WP_Media_List_Table::_get_row_actions()](https://developer.wordpress.org/reference/classes/wp_media_list_table/_get_row_actions/)`wp-admin/includes/class-wp-media-list-table.php` |

Gets the row actions for a media item.

  | 
| [Custom_Image_Header::create_attachment_object()](https://developer.wordpress.org/reference/classes/custom_image_header/create_attachment_object/)`wp-admin/includes/class-custom-image-header.php` |

Creates an attachment ‘object’.

  | 
| [Custom_Image_Header::ajax_header_crop()](https://developer.wordpress.org/reference/classes/custom_image_header/ajax_header_crop/)`wp-admin/includes/class-custom-image-header.php` |

Gets attachment uploaded by Media Manager, crops it, then saves it as a new object. Returns JSON-encoded object details.

  | 
| [Custom_Image_Header::step_3()](https://developer.wordpress.org/reference/classes/custom_image_header/step_3/)`wp-admin/includes/class-custom-image-header.php` |

Displays third step of custom header image page.

  | 
| [_delete_attachment_theme_mod()](https://developer.wordpress.org/reference/functions/_delete_attachment_theme_mod/)`wp-includes/theme.php` |

Checks an attachment being deleted to see if it’s a header or background image.

  | 
| [get_uploaded_header_images()](https://developer.wordpress.org/reference/functions/get_uploaded_header_images/)`wp-includes/theme.php` |

Gets the header images uploaded for the active theme.

  | 
| [get_the_attachment_link()](https://developer.wordpress.org/reference/functions/get_the_attachment_link/)`wp-includes/deprecated.php` |

Retrieve HTML content of attachment image with link.

  | 
| [get_attachment_icon_src()](https://developer.wordpress.org/reference/functions/get_attachment_icon_src/)`wp-includes/deprecated.php` |

Retrieve icon URL and Path.

  | 
| [wp_get_attachment_link()](https://developer.wordpress.org/reference/functions/wp_get_attachment_link/)`wp-includes/post-template.php` |

Retrieves an attachment page link using an image or icon, if possible.

  | 
| [prepend_attachment()](https://developer.wordpress.org/reference/functions/prepend_attachment/)`wp-includes/post-template.php` |

Wraps attachment in paragraph tag before content.

  | 
| [get_post_galleries()](https://developer.wordpress.org/reference/functions/get_post_galleries/)`wp-includes/media.php` |

Retrieves galleries from the passed post’s content.

  | 
| [wp_prepare_attachment_for_js()](https://developer.wordpress.org/reference/functions/wp_prepare_attachment_for_js/)`wp-includes/media.php` |

Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model.

  | 
| [wp_video_shortcode()](https://developer.wordpress.org/reference/functions/wp_video_shortcode/)`wp-includes/media.php` |

Builds the Video shortcode output.

  | 
| [wp_playlist_shortcode()](https://developer.wordpress.org/reference/functions/wp_playlist_shortcode/)`wp-includes/media.php` |

Builds the Playlist shortcode output.

  | 
| [wp_audio_shortcode()](https://developer.wordpress.org/reference/functions/wp_audio_shortcode/)`wp-includes/media.php` |

Builds the Audio shortcode output.

  | 
| [image_get_intermediate_size()](https://developer.wordpress.org/reference/functions/image_get_intermediate_size/)`wp-includes/media.php` |

Retrieves the image’s intermediate size (resized) path, width, and height.

  | 
| [image_downsize()](https://developer.wordpress.org/reference/functions/image_downsize/)`wp-includes/media.php` |

Scales an image to fit a particular size (such as ‘thumb’ or ‘medium’).

  | 
| [redirect_canonical()](https://developer.wordpress.org/reference/functions/redirect_canonical/)`wp-includes/canonical.php` |

Redirects incoming links to the proper URL based on the site url.

  | 
| [wp_xmlrpc_server::_prepare_media_item()](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/_prepare_media_item/)`wp-includes/class-wp-xmlrpc-server.php` |

Prepares media item data for return in an XML-RPC object.

  |

[Show 33 more](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.1.0](https://developer.wordpress.org/reference/since/2.1.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#comment-content-1109)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/#comment-1109)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_url%2F%23comment-1109)
     Vote results for this note: 5[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_url%2F%23comment-1109)
 4.  **Basic Example**
 5.      ```php
         <?php echo wp_get_attachment_url( 12 ); ?> 
         ```
     
 6.  Outputs something like [http://example.net/wp-content/uploads/filename](http://example.net/wp-content/uploads/filename)
 7.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_url%2F%3Freplytocom%3D1109%23feedback-editor-1109)
 8.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/?output_format=md#comment-content-1110)
 9.    [Codex](https://profiles.wordpress.org/codex/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/#comment-1110)
 10. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_url%2F%23comment-1110)
     Vote results for this note: 5[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_url%2F%23comment-1110)
 11. **Use Post Thumbnail as Background Image**
 12.     ```php
         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;
         ```
     
 13.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_url%2F%3Freplytocom%3D1110%23feedback-editor-1110)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_url%2F)
before being able to contribute a note or feedback.