# wp_get_attachment_url()

URL: https://chugunov.pro/api-wordpress/functions/wp_get_attachment_url/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 2.1.0.

## Сигнатура

```php
wp_get_attachment_url( int $attachment_id ): string|false
```

## Описание

Возвращает URL вложения.

## Параметры

- `$attachment_id` `int` — необязательный. ID записи вложения. По умолчанию — глобальный $post.

## Возвращаемое значение

`string|false`

## Исходный код

Файл: `wp-includes/post.php:6969`

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

## История изменений

- 2.1.0 — Introduced.

## Связанные

Использует: [`wp_get_upload_dir`](https://chugunov.pro/api-wordpress/functions/wp_get_upload_dir/), [`_wp_get_attachment_relative_path`](https://chugunov.pro/api-wordpress/functions/_wp_get_attachment_relative_path/), [`is_ssl`](https://chugunov.pro/api-wordpress/functions/is_ssl/), [`set_url_scheme`](https://chugunov.pro/api-wordpress/functions/set_url_scheme/), [`get_the_guid`](https://chugunov.pro/api-wordpress/functions/get_the_guid/), [`wp_basename`](https://chugunov.pro/api-wordpress/functions/wp_basename/), [`trailingslashit`](https://chugunov.pro/api-wordpress/functions/trailingslashit/), [`is_admin`](https://chugunov.pro/api-wordpress/functions/is_admin/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), [`get_post_meta`](https://chugunov.pro/api-wordpress/functions/get_post_meta/), [`get_post`](https://chugunov.pro/api-wordpress/functions/get_post/).
Используется в: [`wp_copy_parent_attachment_properties`](https://chugunov.pro/api-wordpress/functions/wp_copy_parent_attachment_properties/), [`get_media_states`](https://chugunov.pro/api-wordpress/functions/get_media_states/), [`wp_get_original_image_url`](https://chugunov.pro/api-wordpress/functions/wp_get_original_image_url/), [`wp_media_personal_data_exporter`](https://chugunov.pro/api-wordpress/functions/wp_media_personal_data_exporter/), `WP_Widget_Media_Audio::render_media`, `WP_Widget_Media_Video::render_media`, `WP_Widget_Media_Image::render_media`, `WP_Customize_Manager::import_theme_starter_content`, [`get_header_video_url`](https://chugunov.pro/api-wordpress/functions/get_header_video_url/), `WP_REST_Attachments_Controller::prepare_item_for_response`, `WP_Site_Icon::create_attachment_object`, [`export_wp`](https://chugunov.pro/api-wordpress/functions/export_wp/), [`wp_save_image`](https://chugunov.pro/api-wordpress/functions/wp_save_image/), [`_load_image_to_edit_path`](https://chugunov.pro/api-wordpress/functions/_load_image_to_edit_path/), [`edit_form_image_editor`](https://chugunov.pro/api-wordpress/functions/edit_form_image_editor/), [`attachment_submitbox_metadata`](https://chugunov.pro/api-wordpress/functions/attachment_submitbox_metadata/), [`get_attachment_fields_to_edit`](https://chugunov.pro/api-wordpress/functions/get_attachment_fields_to_edit/), [`media_sideload_image`](https://chugunov.pro/api-wordpress/functions/media_sideload_image/), [`image_link_input_fields`](https://chugunov.pro/api-wordpress/functions/image_link_input_fields/), `WP_Media_List_Table::_get_row_actions`, `Custom_Image_Header::create_attachment_object`, `Custom_Image_Header::ajax_header_crop`, `Custom_Image_Header::step_3`, [`_delete_attachment_theme_mod`](https://chugunov.pro/api-wordpress/functions/_delete_attachment_theme_mod/), [`get_uploaded_header_images`](https://chugunov.pro/api-wordpress/functions/get_uploaded_header_images/), [`get_the_attachment_link`](https://chugunov.pro/api-wordpress/functions/get_the_attachment_link/), [`get_attachment_icon_src`](https://chugunov.pro/api-wordpress/functions/get_attachment_icon_src/), [`wp_get_attachment_link`](https://chugunov.pro/api-wordpress/functions/wp_get_attachment_link/), [`prepend_attachment`](https://chugunov.pro/api-wordpress/functions/prepend_attachment/), [`get_post_galleries`](https://chugunov.pro/api-wordpress/functions/get_post_galleries/), [`wp_prepare_attachment_for_js`](https://chugunov.pro/api-wordpress/functions/wp_prepare_attachment_for_js/), [`wp_video_shortcode`](https://chugunov.pro/api-wordpress/functions/wp_video_shortcode/), [`wp_playlist_shortcode`](https://chugunov.pro/api-wordpress/functions/wp_playlist_shortcode/), [`wp_audio_shortcode`](https://chugunov.pro/api-wordpress/functions/wp_audio_shortcode/), [`image_get_intermediate_size`](https://chugunov.pro/api-wordpress/functions/image_get_intermediate_size/), [`image_downsize`](https://chugunov.pro/api-wordpress/functions/image_downsize/), [`redirect_canonical`](https://chugunov.pro/api-wordpress/functions/redirect_canonical/), `wp_xmlrpc_server::_prepare_media_item`.

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/wp_get_attachment_url/
