Title: _wp_get_attachment_relative_path
Published: February 2, 2016
Last modified: May 20, 2026

---

# _wp_get_attachment_relative_path( string $file ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/_wp_get_attachment_relative_path/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/_wp_get_attachment_relative_path/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/_wp_get_attachment_relative_path/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/_wp_get_attachment_relative_path/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_wp_get_attachment_relative_path/?output_format=md#changelog)

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

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Gets the attachment path relative to the upload directory.

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

 `$file`stringrequired

Attachment file name.

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

 string Attachment path relative to the upload directory.

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

    ```php
    function _wp_get_attachment_relative_path( $file ) {
    	$dirname = dirname( $file );

    	if ( '.' === $dirname ) {
    		return '';
    	}

    	if ( str_contains( $dirname, 'wp-content/uploads' ) ) {
    		// Get the directory name relative to the upload directory (back compat for pre-2.7 uploads).
    		$dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 );
    		$dirname = ltrim( $dirname, '/' );
    	}

    	return $dirname;
    }
    ```

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

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

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

Determines if the image meta data is for the image source file.

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

A helper function to calculate the image sources to include in a ‘srcset’ attribute.

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

Retrieves the URL for an attachment.

  |

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

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

## User Contributed Notes

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