How to make a pdf upload field automatically display the pdf
- This topic has 4 replies, 2 voices, and was last updated 3 years, 11 months ago by
alanjacobs.
-
AuthorPosts
-
May 3, 2021 at 1:27 AM #27835
alanjacobs
ParticipantHi, 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
May 3, 2021 at 10:40 PM #27850Long Nguyen
ModeratorHi Alan,
Thank you for getting in touch.
I think it could be done via PHP code. If you use the field type
file
orfile_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-usageThen 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.
May 4, 2021 at 10:06 PM #27906alanjacobs
ParticipantHi,
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
AlanMay 5, 2021 at 6:28 AM #27916Long Nguyen
ModeratorHi Alan,
Did you add the code to the file post template (.php)? Or change the code to
echo do_shortcode("[pdf-embedder url={$url}]");
May 6, 2021 at 4:55 PM #27961alanjacobs
ParticipantHi,
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!
-
AuthorPosts
- You must be logged in to reply to this topic.