• I need to figure out how to use Custom Fields for PDF uploads. So, when a PDF is uploaded in a post, it will automatically place a PDF icon to the left of the link to the PDF file for download.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    You can use the following code to style pdf links with your stylesheet. it gives all pdf links in the post content a class of “pdf”.

    put this in your theme’s functions.php

    function add_pdf_class($content) {
      $patterns = '/<a(.*?)href=["|\'](.*?)([\.pdf|\.PDF])["|\']/';
      $replacements = '<a\1href="\2\3" class="pdf"';
      return preg_replace($patterns, $replacements, $content);
    }
    add_filter('the_content', 'add_pdf_class');

    Thread Starter on_edge

    (@on_edge)

    Thanks, so much.

    Thread Starter on_edge

    (@on_edge)

    Hmmm…found a glitch. I’m going back through old posts that I’ve imported in which the person entering post images left the default link to an image added. A feature I think should be disabled (image links to itself by default). Anyway, the PDF class is being added to the link for JPG post images.

    Moderator keesiemeijer

    (@keesiemeijer)

    Can you give us a link to a webpage which have the pdf class being assigned to images. On my testserver all other links including links to post thumbnails don’t get the pdf class.

    Thread Starter on_edge

    (@on_edge)

    Took me a minute to find one. I cleaned up a lot of them by simply removing the anchor tags. Here you go: https://mstroopers.com/dev/2010/12/son-credits-success-trooper-dad-and-mom/

    Moderator keesiemeijer

    (@keesiemeijer)

    I changed the code a little bit, try it with this:

    function add_pdf_class($content) {
      $patterns = '/<a(.*?)href=["|\'](.*?)\.(pdf|PDF)["|\']/';
      $replacements = '<a\1href="\2.\3" class="pdf"';
      return preg_replace($patterns, $replacements, $content);
    }
    add_filter('the_content', 'add_pdf_class');

    Thread Starter on_edge

    (@on_edge)

    Worked like a charm! Again, thanks. Really appreciate it.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Using Custom Field for PDF Upload in Posts’ is closed to new replies.