wp_get_attachment_caption( int $post_id ): string|false

In this article

Retrieves the caption for an attachment.

Parameters

$post_idintoptional
Attachment ID. Default is the ID of the global $post.

Return

string|false Attachment caption on success, false on failure.

Source

	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.

Changelog

VersionDescription
4.6.0Introduced.

User Contributed Notes

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