Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically convert permalinks to PDFs in search results to the PDF itself, not the Attachment page
<?php
// Automatically convert permalinks to PDFs in search results to the PDF itself, not the Attachment page
function my_force_direct_pdf_links( $permalink ){
global $post;
if ( is_search() && 'application/pdf' == get_post_mime_type( $post->ID ) ) {
// if the result is a PDF, link directly to the file not the attachment page
$permalink = wp_get_attachment_url( $post->ID );
}
return esc_url( $permalink );
}
add_filter( 'the_permalink', 'my_force_direct_pdf_links' );
add_filter( 'attachment_link', 'my_force_direct_pdf_links' );
@2ndkauboy

This comment has been minimized.

Copy link

commented May 30, 2016

The the_permalink function takes only one parameter and the result is escaped using the esc_url function, so this snippet should look like this: https://gist.github.com/2ndkauboy/7cf99e621bfddddd8d237d27f955fbd5

And you might think about avoiding anonymous functions, as they are only available wuth PHP 5.3+ and as far as I can see, you still try to support PHP 5.2 with your plugin.

@jchristopher

This comment has been minimized.

Copy link
Owner Author

commented Nov 7, 2016

@2ndkauboy: Thank you! Sorry for the delay on this, I guess you don't get notified about comments on Gists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.