Last active
November 19, 2020 12:44
-
-
Save jchristopher/b446cc6a3c915c33239ccd0b2d515c36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Link directly to Media files instead of Attachment pages in search results | |
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
I found a solution. I just changed
function my_search_media_direct_link( $permalink, $post )
to
function my_search_media_direct_link( $permalink, $post = null )
so the second argument would be null if the argument was missing.
Hopefully this helps someone else out! Thanks for a great plugin!