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:1649
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' $permalinkbooloptional- Whether to add permalink to image. Default false.Default:
false $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
- Scaling
- Scales with input
- Instructions
- 16–98
- Plugin surface
- 2 hooks
- Called by
- 5
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 127.
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.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 16–19 | get_post(), __() |
!empty($_post) && !wp_get_attachment_url() | 25 | get_post(), wp_get_attachment_url(), __() |
!empty($_post) && wp_get_attachment_url() | 65–70 | get_post(), wp_get_attachment_url(), wp_get_attachment_url(), href(), apply_filters() |
!empty($_post) && wp_get_attachment_url() | 71–76 | get_post(), wp_get_attachment_url(), wp_get_attachment_url(), get_attachment_link(), href(), apply_filters() |
!empty($_post) && wp_get_attachment_url() && $size !== "none" | 76–79 | get_post(), wp_get_attachment_url(), wp_get_attachment_url(), wp_get_attachment_image(), href(), apply_filters() |
!empty($_post) && wp_get_attachment_url() | 78–83 | get_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–85 | get_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–89 | get_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–92 | get_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–98 | get_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 125 | 16–98 | 12 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 127 | 16–98 | 12 | |
| 8.4 | 127 | 16–98 | 12 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 131 | 16–102 | 12 | |
| 8.2 | 131 | 16–102 | 12 | |
| 8.1 | 131 | 16–102 | 12 | |
| 7.4 | 131 | 16–102 | 12 |
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:
- apply_filters( wp_get_attachment_link_attributes )filterline 1687 (+38 into the body)
Filters the list of attachment link attributes.
- apply_filters( wp_get_attachment_link )filterline 1712 (+63 into the body)
Filters a retrieved attachment page link.
Uses · 10
- get_post()Retrieves post data given a post ID or post object.
- wp_get_attachment_url()Retrieves the URL for an attachment.
- __()Retrieves the translation of $text.
- get_attachment_link()Retrieves the permalink for an attachment.
- wp_get_attachment_image()Gets an HTML img element representing an image attachment.
- esc_html()Escaping for HTML blocks.
- get_attached_file()Retrieves attached file path based on attachment ID.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- esc_url()Checks and cleans a URL.
- esc_attr()Escaping for HTML attributes.
Used by · 5
- gallery_shortcode()Builds the Gallery shortcode output.
- get_adjacent_image_link()Gets the next or previous image link that has the same post parent.
- prepend_attachment()Wraps attachment in paragraph tag before content.
- the_attachment_link()Displays an attachment page link using an image or icon.
- wp_playlist_shortcode()Builds the Playlist shortcode output.
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.
Signature, return type and hooks compared across 5 parsed releases.
$post parameter can now accept either a post ID or WP_Post object.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.