Forums

Using Custom Field for PDF Upload in Posts (8 posts)

  1. on_edge
    Member
    Posted 1 year ago #

    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.

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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');
  3. on_edge
    Member
    Posted 1 year ago #

    Thanks, so much.

  4. on_edge
    Member
    Posted 1 year ago #

    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.

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    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.

  6. on_edge
    Member
    Posted 1 year ago #

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

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    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');
  8. on_edge
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic