attachment_linkfilter-hookWP 2.0.0

Allows you to change an attachment link (a file from the media library).

Used in get_attachment_link().

Usage

add_filter( 'attachment_link', 'wp_kama_attachment_link_filter', 10, 2 );

/**
 * Function for `attachment_link` filter-hook.
 * 
 * @param string $link    The attachment's permalink.
 * @param int    $post_id Attachment ID.
 *
 * @return string
 */
function wp_kama_attachment_link_filter( $link, $post_id ){

	// filter...
	return $link;
}
$link(string)
The link to the attachment page.
$post_id(integer)
The attachment ID.

Examples

#1 Replace the media attachment page link with a direct link to the media file

add_filter( 'attachment_link', 'change_attachment_link', 10, 2 );
function change_attachment_link( $link, $post_id ) {
	return wp_get_attachment_url( $post_id );
}

Changelog

Since 2.0.0 Introduced.
Since 5.6.0 Providing an empty string will now disable the view attachment page link on the media modal.

Where the hook is called

get_attachment_link()
attachment_link
wp-includes/link-template.php 534
return apply_filters( 'attachment_link', $link, $post->ID );

Where the hook is used in WordPress

Usage not found.