Custom Fields
Re-Envisioned
Support General How to make a pdf upload field automatically display the pdf

  • Creator
    Topic
  • #27835
    Resolved alanjacobs
    Participant

    Hi, I have a CPT that will include a PDF file.
    I want to display that PDF automatically when the single post template is used. ( I.e it is clicked on from a Posts list page)

    So if I have a WYSIWYG field and upload the PDF there, then I have a plugin - PDFEmbedder - that adds some code. When the single post is chosen the PDF opens automatically, so no need to click on the PDF.

    [pdf-embedder url="https://sitename/wp-content/uploads/2021/04/sample.pdf"]

    But I would rather use the PDF uploader field to better limit what the user uploads. However this field does not pick up the PDFEmbedder code and does not automatically open the PDF.

    Anyone know how I can get the PDF field to auto load a PDF?

    Many thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #27850
    Long Nguyen
    Moderator

    Hi Alan,

    Thank you for getting in touch.

    I think it could be done via PHP code. If you use the field type file or file_upload, please follow the documentation to know how to get the file upload URL.
    https://docs.metabox.io/fields/file/#template-usage
    https://docs.metabox.io/fields/file-upload/#template-usage

    Then execute the shortcode in the single post template file. For example

    $files = rwmb_meta( 'field_id' );
    foreach ( $files as $file ) {
        $url = $file['url'];
        echo do_shortcode('[pdf-embedder url="{$url}"]');
    }

    Hope that makes sense.

    #27906
    alanjacobs
    Participant

    Hi,
    Thanks. Yes it makes perfect sense. But unfortunately I can't get it to work.

    On my CPT I have a file_upload field where I load the PDF file. ( ID=my_pdf_upload_file )

    I then have a text field where I paste in the code.

    $files = rwmb_meta( 'my_pdf_upload_file' );
    foreach ( $files as $file ) {
         $url = $file['url'];
         echo do_shortcode('[pdf-embedder url="{$url}"];
    }
    

    but when I display the page I see the code displayed and an error saying Missing PDF "https://sitename/pdfs/123/%7B$url%7D"
    or sometimes "PDF Embedder requires a url attribute" when I try different quotes or double quotes.

    Not being a coder, well especially not where WordPress is concerned, I'm at a loss.

    Thanks
    Alan

    #27916
    Long Nguyen
    Moderator

    Hi Alan,

    Did you add the code to the file post template (.php)? Or change the code to

    
    echo do_shortcode("[pdf-embedder url={$url}]");
    #27961
    alanjacobs
    Participant

    Hi,
    I have tried all sorts of combinations of quotes and double quotes ... but no change.
    I have added the code into both a text field and a Shortcode field on the Elementor Single Post Template.

    I have reduced the issue down to the pdf-embedder call now, so I do not think this is a metabox issue.

    My code is now simply ...

    
    $mediaurl = 'https://mysite/wp-content/uploads/2021/05/Sample.pdf';
    echo do_shortcode( ' [ pdf-embedder url=$mediaurl ] ' );
    

    But it still generates the Missing PDF error.
    I have raised this with the pdf Embedder team.

    Thanks for your help and sorry to pester you with another plugins problem!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.