|null Null when the attachment is not an image that has an address. */ public static function build( int $attachment_id, string $node_id ): ?array { if ( $attachment_id < 1 || ! wp_attachment_is_image( $attachment_id ) ) { return null; } $source = wp_get_attachment_image_src( $attachment_id, 'full' ); if ( ! is_array( $source ) || '' === (string) $source[0] ) { return null; } $alt = trim( (string) get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ); return array( '@type' => 'ImageObject', '@id' => $node_id, 'url' => $source[0], 'contentUrl' => $source[0], 'width' => (int) $source[1] > 0 ? (int) $source[1] : null, 'height' => (int) $source[2] > 0 ? (int) $source[2] : null, 'caption' => '' !== $alt ? wp_strip_all_tags( $alt ) : null, ); } /** * The address of an attachment's full-size image. * * @since 1.0.0 * * @param int $attachment_id Attachment id. * @return string Empty when there is none. */ public static function url( int $attachment_id ): string { if ( $attachment_id < 1 || ! wp_attachment_is_image( $attachment_id ) ) { return ''; } $source = wp_get_attachment_image_src( $attachment_id, 'full' ); return is_array( $source ) ? (string) $source[0] : ''; } }