wppaste
WordPress

wp_get_attachment_link( int|WP_Post $post = 0, string|int[] $size = 'thumbnail', bool $permalink = false, bool $icon = false, string|false $text = false, array|string $attr = '' ): string

Since
2.5.0, 4.4.0
Source
wp-includes/post-template.php:1652
Retrieves an attachment page link using an image or icon, if possible.

Compatibility

WordPress
since 4.4.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$postint|WP_Postoptional
Post ID or post object.Default: 0
$sizestring|int[]optional
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'thumbnail'.Default: 'thumbnail'
$iconbooloptional
Whether the attachment is an icon. Default false.Default: false
$textstring|falseoptional
Link text to use. Activated by passing a string, false otherwise.
Default false.Default: false
$attrarray|stringoptional
Array or string of attributes. Default empty.Default: ''

Return value

string
HTML content.

Performance profile

How much work a call to wp_get_attachment_link() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
16–98

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 127.

Plugin surface
2 hooks

Third-party callbacks on 'wp_get_attachment_link_attributes', 'wp_get_attachment_link' run inside this call, and their cost is not bounded by anything here.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()one call below wp_get_attachment_link()

Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 10 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_attachment_link() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always16–19get_post(), __()
!empty($_post) && !wp_get_attachment_url()25get_post(), wp_get_attachment_url(), __()
!empty($_post) && wp_get_attachment_url()65–70get_post(), wp_get_attachment_url(), wp_get_attachment_url(), href(), apply_filters()
!empty($_post) && wp_get_attachment_url()71–76get_post(), wp_get_attachment_url(), wp_get_attachment_url(), get_attachment_link(), href(), apply_filters()
!empty($_post) && wp_get_attachment_url() && $size !== "none"76–79get_post(), wp_get_attachment_url(), wp_get_attachment_url(), wp_get_attachment_image(), href(), apply_filters()
!empty($_post) && wp_get_attachment_url()78–83get_post(), wp_get_attachment_url(), wp_get_attachment_url(), get_attached_file(), pathinfo(), esc_html(), href(), apply_filters()
!empty($_post) && wp_get_attachment_url() && $size !== "none"82–85get_post(), wp_get_attachment_url(), wp_get_attachment_url(), get_attachment_link(), wp_get_attachment_image(), href(), apply_filters()
!empty($_post) && wp_get_attachment_url()84–89get_post(), wp_get_attachment_url(), wp_get_attachment_url(), get_attachment_link(), get_attached_file(), pathinfo(), esc_html(), href(), apply_filters()
!empty($_post) && wp_get_attachment_url() && $size !== "none"89–92get_post(), wp_get_attachment_url(), wp_get_attachment_url(), wp_get_attachment_image(), get_attached_file(), pathinfo(), esc_html(), href(), apply_filters()
!empty($_post) && wp_get_attachment_url() && $size !== "none"95–98get_post(), wp_get_attachment_url(), wp_get_attachment_url(), get_attachment_link(), wp_get_attachment_image(), get_attached_file(), pathinfo(), esc_html(), href(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12516–98122 fewer instructions than PHP 8.5
8.512716–9812
8.412716–98124 fewer instructions than PHP 8.3
8.313116–10212
8.213116–10212
8.113116–10212
7.413116–10212

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_get_attachment_link() runs, in this order:

  1. apply_filters( wp_get_attachment_link_attributes )filterline 1690 (+38 into the body)

    Filters the list of attachment link attributes.

  2. apply_filters( wp_get_attachment_link )filterline 1715 (+63 into the body)

    Filters a retrieved attachment page link.

Uses · 10

Used by · 5

Source code

function wp_get_attachment_link( $post = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {	$_post = get_post( $post ); 	if ( empty( $_post ) || ( 'attachment' !== $_post->post_type ) || ! wp_get_attachment_url( $_post->ID ) ) {		return __( 'Missing Attachment' );	} 	$url = wp_get_attachment_url( $_post->ID ); 	if ( $permalink ) {		$url = get_attachment_link( $_post->ID );	} 	if ( $text ) {		$link_text = $text;	} elseif ( $size && 'none' !== $size ) {		$link_text = wp_get_attachment_image( $_post->ID, $size, $icon, $attr );	} else {		$link_text = '';	} 	if ( '' === trim( $link_text ) ) {		$link_text = $_post->post_title;	} 	if ( '' === trim( $link_text ) ) {		$link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) );	} 	/**	 * Filters the list of attachment link attributes.	 *	 * @since 6.2.0	 *	 * @param array $attributes An array of attributes for the link markup,	 *                          keyed on the attribute name.	 * @param int   $id         Post ID.	 */	$attributes = apply_filters( 'wp_get_attachment_link_attributes', array( 'href' => $url ), $_post->ID ); 	$link_attributes = '';	foreach ( $attributes as $name => $value ) {		$value            = 'href' === $name ? esc_url( $value ) : esc_attr( $value );		$link_attributes .= ' ' . esc_attr( $name ) . "='" . $value . "'";	} 	$link_html = "<a$link_attributes>$link_text</a>"; 	/**	 * Filters a retrieved attachment page link.	 *	 * @since 2.7.0	 * @since 5.1.0 Added the `$attr` parameter.	 *	 * @param string       $link_html The page link HTML output.	 * @param int|WP_Post  $post      Post ID or object. Can be 0 for the current global post.	 * @param string|int[] $size      Requested image size. Can be any registered image size name, or	 *                                an array of width and height values in pixels (in that order).	 * @param bool         $permalink Whether to add permalink to image. Default false.	 * @param bool         $icon      Whether to include an icon.	 * @param string|false $text      If string, will be link text.	 * @param array|string $attr      Array or string of attributes.	 */	return apply_filters( 'wp_get_attachment_link', $link_html, $post, $size, $permalink, $icon, $text, $attr );}

Changelog

Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

4.4.0
The $post parameter can now accept either a post ID or WP_Post object.from the docblock
2.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/post-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.