WP_REST_Attachments_Controller::get_attachment_filenameprotectedWP 7.0.0

Gets the attachment's original file name.

Method of the class: WP_REST_Attachments_Controller{}

No Hooks.

Returns

String|null. Attachment file name, or null if not found.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_attachment_filename( $attachment_id ): ?string;
$attachment_id(int) (required)
Attachment ID.

Changelog

Since 7.0.0 Introduced.

WP_REST_Attachments_Controller::get_attachment_filename() code WP 7.0

protected function get_attachment_filename( int $attachment_id ): ?string {
	$path = wp_get_original_image_path( $attachment_id );

	if ( $path ) {
		return wp_basename( $path );
	}

	$path = get_attached_file( $attachment_id );

	if ( $path ) {
		return wp_basename( $path );
	}

	return null;
}