Skip to content

Instantly share code, notes, and snippets.

@chasecmiller
Created November 1, 2017 22:03
Show Gist options
  • Save chasecmiller/6853527a53a2a86a9f5d4fc3ca59a963 to your computer and use it in GitHub Desktop.
Save chasecmiller/6853527a53a2a86a9f5d4fc3ca59a963 to your computer and use it in GitHub Desktop.
WordPress: Change PDF links in WordPress to link directly to the PDF instead of the attachment page
<?php
defined('ABSPATH') || exit(1);
add_filter('the_permalink', 'thePermalinkPDF');
add_filter('attachment_link', 'thePermalinkPDF');
// Automatically convert permalinks to PDFs in search results to the PDF itself, not the Attachment page
function thePermalinkPDF($permalink){
global $post;
if (get_post_mime_type($post->ID) != 'application/pdf') {
return $permalink;
}
// if the result is a PDF, link directly to the file not the attachment page
return esc_url(wp_get_attachment_url( $post->ID ));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment