_wp_get_attachment_relative_path( string $file ): string

In this article

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

Gets the attachment path relative to the upload directory.

Parameters

$filestringrequired
Attachment file name.

Return

string Attachment path relative to the upload directory.

Source

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

Changelog

VersionDescription
4.4.1Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.