Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active November 19, 2020 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/b446cc6a3c915c33239ccd0b2d515c36 to your computer and use it in GitHub Desktop.
Save jchristopher/b446cc6a3c915c33239ccd0b2d515c36 to your computer and use it in GitHub Desktop.
<?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 );
@cmwint
Copy link

cmwint commented Mar 14, 2017

When I use this, it works great. (thank you!) However, my comment_form() function errors out on this function. Here is the error I am getting:

Warning: Missing argument 2 for my_search_media_direct_link()

This is the exact function I am using (same as the example above) in functions.php:

// Link directly to Media files instead of Attachment pages in search results
    function my_search_media_direct_link( $permalink, $post ) {
    if ( is_search() && 'attachment' === get_post_type( $post ) ) {
         $permalink = wp_get_attachment_url( $post['ID'] );
     }
    return esc_url( $permalink );
 }
 add_filter( 'the_permalink', 'my_search_media_direct_link', 10, 2 );

And this is where it's erroring, in comments.php at the last line:

$args = array(
    'comment_notes_after' => '',
    'comment_field' => '<textarea placeholder="Comment" cols="45" rows="8" name="comment" aria-required="true"></textarea>'

);
comment_form($args);

Any ideas about what's going wrong? Thank you for the help.

@cmwint
Copy link

cmwint commented Mar 16, 2017

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment