Home / admin / Attach PDF files to post with custom metabox file selection
  • How to Attach PDF files to post with custom metabox file selection


    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 ) ( click code to copy )

    wordpress snippet : PHP

    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>';
    	      echo '<option value="'.$pdf[]= $file->guid.'">'.$pdf[]= $file->guid.'</option>';
    	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">
    		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;}
    function pdf_file_url(){
    	global $wp_query;
    	$custom = get_post_custom($wp_query->post->ID);
    	echo $custom['link'][0];
    ( click code to copy )

    wordpress snippet : PHP

    <? pdf_file_url(); ?>
    ( click code to copy )

    wordpress snippet : PHP - in use sample

    <a href="<? pdf_file_url(); ?>">My PDF File</a>

    ( WordPress codex functions, hooks, in this snippet. )

    posts_per_page, query, admin_head, init, save_post, get_post, get_post_custom, update_post_meta, add_meta_box, add_action, selected, wp,
  • 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…

  • Kevin I found the solution : 

    Replace the following : 

    echo ‘SELECT pdf FILE’;

    with this : 

    echo ”;

    • patrick1991groot

      I know this is a very old post but the right solution would be to pass a empty value i guess!

      Replace the following :

      echo ‘SELECT pdf FILE’;

      with this :

      echo ‘SELECT pdf FILE’;

  • 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

  • jiweb

    found it very useful,is there any way to display file name instead of just PDF File:?

    • sturnel

      just replace

      $pdf[]= $file->guid


      $pdf[]= $file->post_title

  • bob

    your code breaks my site.

  • david

    Great job! But if I try to add multiple Metabox I end up getting the same file for every Custom filed.. I tried making different functions like: function pdf2_ Any ideas?

  • Luigi

    sorry for my english:

    you can integrate this code for a page? what are the changes to be done?

    I did a test back into an post but I do not work that the link is not make the connection with my file help please!!

  • Luigi

    RESOLVED!!! the snippet missing the “php”

  • Craig

    Hi, since upgrading to WP3.5 this has disappeared from my edit posts/pages, so I can no longer add/edit PDF’s. Can anyone help?

    Thanks, Craig

  • Kai

    hi mate, i have a custom post type , listing, i cannot add the above php code to it to display the PDF, when i add this code to functions.php then it gets added for my Posts but not my listings .


  • belacqua16

    Hi, thank you for the script. I’m using it with a custom post type.
    Display is correct but I can’t save the pdf. Rather, it does save it the first time but then I cannot change it. So I guess it is the ‘save’ function:

    function save_pdf_link(){

    global $post;
    if (defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE){ return $post->ID; }
    update_post_meta($post->ID, “link”, $_POST[“link”]);

    My custom type it is called ‘events’. Any idea to make it work?

    Thank You

  • Shreyo Gi

    thanks for the code. :)

  • Franklin

    The isn’t working in loop.

    • Franklin

      The <? pdf_file_url(); ?> isn't working in loop.

  • Hi, i need add mor mime type, it’s possible?.

    • You can do an array for the mime, eg:

      ‘post_mime_type’ => array(‘application/doc’,’application/pdf’)