Media: Introduce wp_get_attachment_link_attributes filter.

This changeset introduces the `wp_get_attachment_link_attributes` hook to allow developers to filter the link attributes when getting the attachment link.

Props NathanAtmoz, aaroncampbell, antpb, costdev.
Fixes #41574.


git-svn-id: https://develop.svn.wordpress.org/trunk@55262 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras
2023-02-07 14:33:44 +00:00
parent 3ba96e225d
commit 67662cbe1b
2 changed files with 124 additions and 1 deletions

View File

@@ -1666,7 +1666,24 @@ function wp_get_attachment_link( $post = 0, $size = 'thumbnail', $permalink = fa
$link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) );
}
$link_html = "<a href='" . esc_url( $url ) . "'>$link_text</a>";
/**
* 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.