Viewing 3 reply threads
  • Author
    Posts
    • #6406
      bradrh
      Member

      Hello, Piklist seems really helpful in numerous ways I’ve been needing, but I’m new to WP and PHP and I’ve been having trouble accessing my fields on the front end of my site. I’m trying to create a “download” button so a user can download a pdf that I upload using the Media Uploader.

      Here’s my meta-box:

      <?php
      /*
      Title: Please upload a downloadable Handout 
      Post Type: handout
      */
      // Media Uploader
        piklist('field', array(
          'type' => 'file'
          ,'field' => 'upload_media'
          ,'label' => __('Add a .pdf')
          ,'options' => array(
            'modal_title' => __('Add File')
            ,'button' => __('Add')
          )
        ));
      ?>

      and here’s what I’m trying in my template:

      <a href="
      <?php the_permalink();?>/<?php get_post( $post->ID, 'upload_media', true ); ?>
      " class="button">Download Handout</a>

      This just goes to the post, using single.php.

      and:

      <a href="
      <?php
      $myupload = get_post( $post->ID, 'upload_media', true ); 
      $link = $myupload->upload_media;
      echo 'link:' . $link;
      print_r($myupload);
      ?>
      " class="button">Download Handout</a>

      This prints out all that meta data in the browser’s address bar.

      I saw somewhere in the User Guide maybe I could use wp_get_attachment_metadata( $attachment_id, $unfiltered ); but I don’t know how to use it.

      Thanks for any advice!

    • #6407
      Steve
      Keymaster

      @bradrh– Welcome to the Piklist community!

      I think you’re having issues because of the way WordPress handles file uploads. WordPress creates an “attachment” (a type of Post), for each file uploaded.

      Try this tutorial. It might help.

    • #6408
      bradrh
      Member

      Hi Steve, Thank you and thanks for the response.

      I was referring to that tutorial before. That’s where I got my second code block. But it refers to images and I’m having trouble implementing it. I don’t want to show the doc, just add it’s URL to the anchor’s href.

      The bottom part of the tutorial mentions the URL:
      “To retrieve the file data you can use the standard WordPress function wp_get_attachment_metadata. This function will give you the width, height, file path, sizes, image_meta (aperture, camera, etc) of the uploaded file.”

      Could you show me what that code would look like, grabbing the file path, and adding it to a wordpress template?

      Thank you for your time!

    • #6410
      Steve
      Keymaster

      Try this (untested):

      $metadata = wp_get_attachment_metadata($post->ID);
      $file = $metadata['file'];
      $url = site_url() . $file;
      
      echo '< img src="' . $url . '" />';
      
Viewing 3 reply threads
  • You must be logged in to reply to this topic.