_wp_get_attachment_relative_path()WP 4.4.1

Gets the attachment path relative to the upload directory.

Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.

No Hooks.

Return

String. Attachment path relative to the upload directory.

Usage

_wp_get_attachment_relative_path( $file );
$file(string) (required)
Attachment file name.

Changelog

Since 4.4.1 Introduced.

_wp_get_attachment_relative_path() code WP 6.7.1

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