Forked from pommiegranit/link_pdf_attachment.php
Last active
August 29, 2015 14:21
-
-
Save moughamir/8424ebdf135ac7c7410c 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 | |
/* | |
Plugin Name: Link PDF Attachment | |
Plugin URI: http://premium.wpmudev.org | |
Description: Adds a link to the top of a WordPress post to the first PDF attachment | |
Author: Chris Knowles | |
Version: 1.0 | |
Author URI: http://twitter.com/ChrisKnowles | |
*/ | |
function pdf_add_link( $content ) { | |
global $post; | |
if ( !is_single() ) return $content; | |
$args = array( | |
'numberposts' => 1, | |
'order' => 'ASC', | |
'post_mime_type' => 'application/pdf', | |
'post_parent' => $post->ID, | |
'post_status' => null, | |
'post_type' => 'attachment', | |
); | |
$attachments = get_children( $args ); | |
if ( $attachments ) { | |
foreach ( $attachments as $attachment ) { | |
$content = '<div class="pdf_download"><a href="' . wp_get_attachment_url( $attachment->ID ) . '" target="_blank" >Download article as PDF</a></div>' . $content; | |
} | |
} | |
return $content; | |
} | |
add_filter( 'the_content' , 'pdf_add_link' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment