Skip to content

Instantly share code, notes, and snippets.

@searchwpgists
Created March 9, 2022 17:06
Show Gist options
  • Save searchwpgists/218dc4d69cbe63b0a9b9326ce113bc28 to your computer and use it in GitHub Desktop.
Save searchwpgists/218dc4d69cbe63b0a9b9326ce113bc28 to your computer and use it in GitHub Desktop.
<?php
// Link directly to Media files instead of Attachment pages in search results
// @link https://searchwp.com/documentation/knowledge-base/link-file-pdf/
function my_search_media_direct_link( $permalink, $post = null ) {
if ( ( is_search() || doing_action( 'wp_ajax_searchwp_live_search' )
|| doing_action( 'wp_ajax_nopriv_searchwp_live_search' ) )
&& 'attachment' === get_post_type( $post ) ) {
$permalink = wp_get_attachment_url( $post );
}
return esc_url( $permalink );
}
add_filter( 'the_permalink', 'my_search_media_direct_link', 99, 2 );
add_filter( 'attachment_link', 'my_search_media_direct_link', 99, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment