get_attachment_link( int|WP_Post $post = null, bool $leavename = false ): string
- Since
- 2.0.0
- Source
wp-includes/link-template.php:472
Description
This can be used in the WordPress Loop or outside of it.
Compatibility
- WordPress
- since 2.0.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 object. Default uses the global
$post.Default:null $leavenamebooloptional- Whether to keep the page name. Default false.Default:
false
Return value
string- The attachment permalink.
Performance profile
How much work a call to 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
- Constant
- Instructions
- 30–92
- Plugin surface
- 1 hook
- Called by
- 13
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 115.
Third-party callbacks on 'attachment_link' run inside this call, and their cost is not bounded by anything here.
13 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 - optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below get_attachment_link() - serializeserialisation
maybe_unserialize()one call below get_attachment_link()
Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 88 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_attachment_link() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$post | 30–37 | get_post(), wp_force_plain_post_permalink(), apply_filters() |
$post | 34–41 | get_post(), wp_force_plain_post_permalink(), get_post(), apply_filters() |
!$post | 35–43 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), ->using_permalinks(), apply_filters() |
!$post | 36–43 | get_post(), wp_force_plain_post_permalink(), home_url(), apply_filters() |
$post | 39–47 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), ->using_permalinks(), apply_filters() |
$post | 40–47 | get_post(), wp_force_plain_post_permalink(), get_post(), home_url(), apply_filters() |
!$post | 41–49 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), ->using_permalinks(), home_url(), apply_filters() |
| always | 42–44 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), apply_filters() |
!$post && ->using_permalinks() | 45–52 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), ->using_permalinks(), user_trailingslashit(), home_url(), apply_filters() |
$post | 45–53 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), ->using_permalinks(), home_url(), apply_filters() |
$post | 46–48 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), apply_filters() |
| always | 47–50 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), ->using_permalinks(), apply_filters() |
76 further outcomes, up to 92 instructions
| always | 48–50 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), home_url(), apply_filters() |
$post && ->using_permalinks() | 49–56 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), ->using_permalinks(), user_trailingslashit(), home_url(), apply_filters() |
!$post && ->using_permalinks() && $parentlink | 51–61 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), get_permalink(), apply_filters() |
!$post && ->using_permalinks() | 51–58 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), ->using_permalinks(), user_trailingslashit(), home_url(), home_url(), apply_filters() |
$post | 51–54 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), ->using_permalinks(), apply_filters() |
!$post && ->using_permalinks() && $parentlink | 52–62 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), _get_page_link(), apply_filters() |
$post | 52–54 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), home_url(), apply_filters() |
| always | 53–56 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), ->using_permalinks(), home_url(), apply_filters() |
!$post && ->using_permalinks() && $parentlink | 54–66 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), get_permalink(), get_option(), apply_filters() |
!$post && ->using_permalinks() && $parentlink | 55–67 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), _get_page_link(), get_option(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 55–65 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), get_permalink(), apply_filters() |
$post && ->using_permalinks() | 55–62 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), ->using_permalinks(), user_trailingslashit(), home_url(), home_url(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 56–66 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), _get_page_link(), apply_filters() |
!$post && ->using_permalinks() && $parentlink | 57–67 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), get_permalink(), home_url(), apply_filters() |
->using_permalinks() | 57–59 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), ->using_permalinks(), user_trailingslashit(), home_url(), apply_filters() |
$post | 57–60 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), ->using_permalinks(), home_url(), apply_filters() |
!$post && ->using_permalinks() && $parentlink | 58–68 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), _get_page_link(), home_url(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 58–70 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), get_permalink(), get_option(), apply_filters() |
!$post && ->using_permalinks() && !$parentlink | 59–69 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), get_permalink(), trailingslashit(), user_trailingslashit(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 59–71 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), _get_page_link(), get_option(), apply_filters() |
!$post && ->using_permalinks() && $parentlink | 60–72 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), get_permalink(), get_option(), home_url(), apply_filters() |
!$post && ->using_permalinks() && !$parentlink | 60–70 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), _get_page_link(), trailingslashit(), user_trailingslashit(), apply_filters() |
!$post && ->using_permalinks() && $parentlink | 61–73 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), _get_page_link(), get_option(), home_url(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 61–71 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), get_permalink(), home_url(), apply_filters() |
$post && ->using_permalinks() | 61–63 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), ->using_permalinks(), user_trailingslashit(), home_url(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 62–72 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), _get_page_link(), home_url(), apply_filters() |
!$post && ->using_permalinks() && !$parentlink | 62–74 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), get_permalink(), get_option(), trailingslashit(), user_trailingslashit(), apply_filters() |
->using_permalinks() && $parentlink | 63–68 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), apply_filters() |
!$post && ->using_permalinks() && !$parentlink | 63–75 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), _get_page_link(), get_option(), trailingslashit(), user_trailingslashit(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 63–73 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), get_permalink(), trailingslashit(), user_trailingslashit(), apply_filters() |
->using_permalinks() | 63–65 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), ->using_permalinks(), user_trailingslashit(), home_url(), home_url(), apply_filters() |
->using_permalinks() && $parentlink | 64–69 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 64–76 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), get_permalink(), get_option(), home_url(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 64–74 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), _get_page_link(), trailingslashit(), user_trailingslashit(), apply_filters() |
!$post && ->using_permalinks() && !$parentlink | 65–75 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), get_permalink(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 65–77 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), _get_page_link(), get_option(), home_url(), apply_filters() |
!$post && ->using_permalinks() && !$parentlink | 66–76 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), _get_page_link(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
->using_permalinks() && $parentlink | 66–73 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), get_option(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 66–78 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), get_permalink(), get_option(), trailingslashit(), user_trailingslashit(), apply_filters() |
->using_permalinks() && $parentlink | 67–74 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), get_option(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 67–72 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 67–79 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), _get_page_link(), get_option(), trailingslashit(), user_trailingslashit(), apply_filters() |
$post && ->using_permalinks() | 67–69 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), ->using_permalinks(), user_trailingslashit(), home_url(), home_url(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 68–73 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), apply_filters() |
!$post && ->using_permalinks() && !$parentlink | 68–80 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), get_permalink(), get_option(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
->using_permalinks() && $parentlink | 69–74 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), home_url(), apply_filters() |
!$post && ->using_permalinks() && !$parentlink | 69–81 | get_post(), wp_force_plain_post_permalink(), ->using_permalinks(), _get_page_link(), get_option(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 69–79 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), get_permalink(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
->using_permalinks() && $parentlink | 70–75 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), home_url(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 70–80 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), _get_page_link(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 70–77 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), get_option(), apply_filters() |
->using_permalinks() && !$parentlink | 71–76 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), trailingslashit(), user_trailingslashit(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 71–78 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), get_option(), apply_filters() |
->using_permalinks() && $parentlink | 72–79 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), get_option(), home_url(), apply_filters() |
->using_permalinks() && !$parentlink | 72–77 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), trailingslashit(), user_trailingslashit(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 72–84 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), get_permalink(), get_option(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
->using_permalinks() && $parentlink | 73–80 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), get_option(), home_url(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 73–78 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), home_url(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 73–85 | get_post(), wp_force_plain_post_permalink(), get_post(), ->using_permalinks(), _get_page_link(), get_option(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 74–79 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), home_url(), apply_filters() |
->using_permalinks() && !$parentlink | 74–81 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), get_option(), trailingslashit(), user_trailingslashit(), apply_filters() |
->using_permalinks() && !$parentlink | 75–82 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), get_option(), trailingslashit(), user_trailingslashit(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 75–80 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), trailingslashit(), user_trailingslashit(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 76–83 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), get_option(), home_url(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 76–81 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), trailingslashit(), user_trailingslashit(), apply_filters() |
->using_permalinks() && !$parentlink | 77–82 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
$post && ->using_permalinks() && $parentlink | 77–84 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), get_option(), home_url(), apply_filters() |
->using_permalinks() && !$parentlink | 78–83 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 78–85 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), get_option(), trailingslashit(), user_trailingslashit(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 79–86 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), get_option(), trailingslashit(), user_trailingslashit(), apply_filters() |
->using_permalinks() && !$parentlink | 80–87 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), get_option(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
->using_permalinks() && !$parentlink | 81–88 | get_post(), wp_force_plain_post_permalink(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), get_option(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 81–86 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 82–87 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 84–91 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), get_permalink(), get_option(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
$post && ->using_permalinks() && !$parentlink | 85–92 | get_post(), wp_force_plain_post_permalink(), get_post(), get_post_type(), is_post_type_viewable(), ->using_permalinks(), _get_page_link(), get_option(), trailingslashit(), user_trailingslashit(), home_url(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 114 | 30–91 | 17 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 115 | 30–92 | 17 | |
| 8.4 | 115 | 30–92 | 17 | 11 fewer instructions than PHP 8.3 |
| 8.3 | 126 | 30–103 | 17 | |
| 8.2 | 126 | 30–103 | 17 | |
| 8.1 | 126 | 30–103 | 17 | |
| 7.4 | 126 | 30–103 | 17 |
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 · 1
One hook fires while get_attachment_link() runs, in this order:
- apply_filters( attachment_link )filterline 534 (+62 into the body)
Filters the permalink for an attachment.
Uses · 12
- get_post()Retrieves post data given a post ID or post object.
- wp_force_plain_post_permalink()Determine whether post should always use a plain permalink structure.
- is_post_type_viewable()Determines whether a post type is considered "viewable".
- get_post_type()Retrieves the post type of the current post or of a given post.
- _get_page_link()Retrieves the page permalink.
- get_permalink()Retrieves the full permalink for the current post or post ID.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- get_option()Retrieves an option value based on an option name.
- user_trailingslashit()Retrieves a trailing-slashed string if the site is set for adding trailing slashes.
- trailingslashit()Appends a trailing slash.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 13
- WP_Widget_Media_Image::render_media()Render the media on the frontend.
- _fix_attachment_links()Replaces hrefs of attachment anchors with up-to-date permalinks.
- get_permalink()Retrieves the full permalink for the current post or post ID.
- get_the_attachment_link()Retrieve HTML content of attachment image with link.
- image_link_input_fields()Retrieves HTML for the Link URL buttons with the default link type as specified.
- image_media_send_to_editor()Retrieves the media element HTML to send to the editor.
- media_upload_form_handler()Handles form submissions for the legacy media uploader.
- redirect_canonical()Redirects incoming links to the proper URL based on the site url.
- twentyfourteen_the_attached_image()Print the attached image with a link to the next attached image.
- twentythirteen_the_attached_image()Print the attached image with a link to the next attached image.
- wp_ajax_send_attachment_to_editor()Handles sending an attachment to the editor via AJAX.
- wp_get_attachment_link()Retrieves an attachment page link using an image or icon, if possible.
Show all 13
- wp_prepare_attachment_for_js()Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model.
Source code
function get_attachment_link( $post = null, $leavename = false ) { global $wp_rewrite; $link = false; $post = get_post( $post ); $force_plain_link = wp_force_plain_post_permalink( $post ); $parent_id = $post->post_parent; $parent = $parent_id ? get_post( $parent_id ) : false; $parent_valid = true; // Default for no parent. if ( $parent_id && ( $post->post_parent === $post->ID || ! $parent || ! is_post_type_viewable( get_post_type( $parent ) ) ) ) { // Post is either its own parent or parent post unavailable. $parent_valid = false; } if ( $force_plain_link || ! $parent_valid ) { $link = false; } elseif ( $wp_rewrite->using_permalinks() && $parent ) { if ( 'page' === $parent->post_type ) { $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front. } else { $parentlink = get_permalink( $post->post_parent ); } if ( is_numeric( $post->post_name ) || str_contains( get_option( 'permalink_structure' ), '%category%' ) ) { $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker. } else { $name = $post->post_name; } if ( ! str_contains( $parentlink, '?' ) ) { $link = user_trailingslashit( trailingslashit( $parentlink ) . '%postname%' ); } if ( ! $leavename ) { $link = str_replace( '%postname%', $name, $link ); } } elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) { $link = home_url( user_trailingslashit( $post->post_name ) ); } if ( ! $link ) { $link = home_url( '/?attachment_id=' . $post->ID ); } /** * Filters the permalink for an attachment. * * @since 2.0.0 * @since 5.6.0 Providing an empty string will now disable * the view attachment page link on the media modal. * * @param string $link The attachment's permalink. * @param int $post_id Attachment ID. */ return apply_filters( 'attachment_link', $link, $post->ID );}Changelog
Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/link-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.