Created
November 1, 2017 22:03
-
-
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
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 | |
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