updated   : May 15, 2012
we now have 588 snippets
By : , on September 30, 2011 9:00 am

Attach PDF files to post with custom metabox file selection

wordpress snippet

Adding this snippet to the functions.php of your wordpress theme will create a new metabox within your post editing screen with a select menu listing all PDF files. Adding the second snippet in your wordpress template in the location you wish to display the files URL.

( example screenshot )
snippet :  PHPcopy
add_action("admin_init", "pdf_init");
add_action('save_post', 'save_pdf_link');
function pdf_init(){
	add_meta_box("my-pdf", "PDF Document", "pdf_link", "post", "normal", "low");
	}
function pdf_link(){
	global $post;
	$custom  = get_post_custom($post->ID);
	$link    = $custom["link"][0];
	$count   = 0;
	echo '<div class="link_header">';
	$query_pdf_args = array(
		'post_type' => 'attachment',
		'post_mime_type' =>'application/pdf',
		'post_status' => 'inherit',
		'posts_per_page' => -1,
		);
	$query_pdf = new WP_Query( $query_pdf_args );
	$pdf = array();
	echo '<select name="link">';
	echo '<option class="pdf_select">SELECT pdf FILE</option>';
	foreach ( $query_pdf->posts as $file) {
	   if($link == $pdf[]= $file->guid){
	      echo '<option value="'.$pdf[]= $file->guid.'" selected="true">'.$pdf[]= $file->guid.'</option>';
		 }else{
	      echo '<option value="'.$pdf[]= $file->guid.'">'.$pdf[]= $file->guid.'</option>';
		 }
		$count++;
	}
	echo '</select><br /></div>';
	echo '<p>Selecting a pdf file from the above list to attach to this post.</p>';
	echo '<div class="pdf_count"><span>Files:</span> <b>'.$count.'</b></div>';
}
function save_pdf_link(){
	global $post;
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ return $post->ID; }
	update_post_meta($post->ID, "link", $_POST["link"]);
}
add_action( 'admin_head', 'pdf_css' );
function pdf_css() {
	echo '<style type="text/css">
	.pdf_select{
		font-weight:bold;
		background:#e5e5e5;
		}
	.pdf_count{
		font-size:9px;
		color:#0066ff;
		text-transform:uppercase;
		background:#f3f3f3;
		border-top:solid 1px #e5e5e5;
		padding:6px 6px 6px 12px;
		margin:0px -6px -8px -6px;
		-moz-border-radius:0px 0px 6px 6px;
		-webkit-border-radius:0px 0px 6px 6px;
		border-radius:0px 0px 6px 6px;
		}
	.pdf_count span{color:#666;}
		</style>';
}
function pdf_file_url(){
	global $wp_query;
	$custom = get_post_custom($wp_query->post->ID);
	echo $custom['link'][0];
}
snippet :  PHPcopy
<? pdf_file_url(); ?>
snippet :  PHP - in use samplecopy
<a href="<? pdf_file_url(); ?>">My PDF File</a>
  • http://twitter.com/leannekera Leanne Borrowman

    I can think of so many websites that would benefit from this!

    How would I go about including this into an if statement? So if a pdf has been uploaded, show the link for example…

    • http://wpsnipp.com Kevin Chard

      Could could just replace the pdf_file_url function with the following that would set things up for you. http://pastebin.com/KZmigAyF

      • http://twitter.com/leannekera Leanne Borrowman

        Thankyou so much Kevin, just tested it and that works like a charm :) amazing – will re-tweet

        • http://wpsnipp.com Kevin Chard

          Cool glad to hear I could help.

  • http://twitter.com/leannekera Leanne Borrowman

    Kevin I found the solution : 

    Replace the following : 

    echo ’SELECT pdf FILE’;

    with this : 

    echo ”;

  • http://www.kavamediagh.com/ Amazing

    Hi Kevin,

    Got in here upon a search and the pdf_link function came in handy to fix as a part of a custom theme and post types am working on.

    Thank you, you saved me hours.

  • John

    This is a fantastic post, extremely useful. I am trying to adapt it to enable multiple PDF’s to be selected and then output but struggling, what would be the best way to approach? Any advice appreciated, and thanks again for the article