Está procurando uma maneira de criar uma nova metabox na tela de edição de publicações com um menu de seleção? Embora provavelmente exista um plug-in para isso, criamos um trecho de código rápido que você pode usar para anexar arquivos PDF à postagem com a seleção de arquivo de metabox personalizada.
Instruções:
Tudo o que você precisa fazer é adicionar esse código ao arquivo functions.php do seu tema ou em um plug-in específico do site:
add_action("admin_init", "pdf_init"); add_action('save_post', 'save_pdf_link'); function pdf_init(){ add_meta_box("my-pdf", "PDF Document", "pdf_link", "post", "normal", "low"); } function pdf_link(){ global $post; $custom = get_post_custom($post->ID); $link = $custom["link"][0]; $count = 0; echo '<div class="link_header">'; $query_pdf_args = array( 'post_type' => 'attachment', 'post_mime_type' =>'application/pdf', 'post_status' => 'inherit', 'posts_per_page' => -1, ); $query_pdf = new WP_Query( $query_pdf_args ); $pdf = array(); echo '<select name="link">'; echo '<option class="pdf_select">SELECT pdf FILE</option>'; foreach ( $query_pdf->posts as $file) { if($link == $pdf[]= $file->guid){ echo '<option value="'.$pdf[]= $file->guid.'" selected="true">'.$pdf[]= $file->guid.'</option>'; }else{ echo '<option value="'.$pdf[]= $file->guid.'">'.$pdf[]= $file->guid.'</option>'; } $count++; } echo '</select><br /></div>'; echo '<p>Selecting a pdf file from the above list to attach to this post.</p>'; echo '<div class="pdf_count"><span>Files:</span> <b>'.$count.'</b></div>'; } function save_pdf_link(){ global $post; if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ return $post->ID; } update_post_meta($post->ID, "link", $_POST["link"]); } add_action( 'admin_head', 'pdf_css' ); function pdf_css() { echo '<style type="text/css"> .pdf_select{ font-weight:bold; background:#e5e5e5; } .pdf_count{ font-size:9px; color:#0066ff; text-transform:uppercase; background:#f3f3f3; border-top:solid 1px #e5e5e5; padding:6px 6px 6px 12px; margin:0px -6px -8px -6px; -moz-border-radius:0px 0px 6px 6px; -webkit-border-radius:0px 0px 6px 6px; border-radius:0px 0px 6px 6px; } .pdf_count span{color:#666;} </style>'; } function pdf_file_url(){ global $wp_query; $custom = get_post_custom($wp_query->post->ID); echo $custom['link'][0]; }
Para exibir o arquivo PDF, você pode usar este código:
<? pdf_file_url(); ?>
<a href="<? pdf_file_url(); ?>">My PDF File</a>
Observação: Se esta é a primeira vez que você adiciona trechos de código no WordPress, consulte nosso guia sobre como copiar/colar corretamente trechos de código no WordPress, para não danificar acidentalmente seu site.
Se você gostou desse snippet de código, considere dar uma olhada em nossos outros artigos no site, como: 20 melhores temas de igreja do WordPress para expandir o rebanho e como rastrear links de afiliados no Google Analytics.
Hi, i need add mor mime type, it’s possible?.
You can do an array for the mime, eg:
‘post_mime_type’ => array(‘application/doc’,’application/pdf’)
The isn’t working in loop.
The <? pdf_file_url(); ?> isn't working in loop.
thanks for the code. 🙂
Hi, thank you for the script. I’m using it with a custom post type.
Display is correct but I can’t save the pdf. Rather, it does save it the first time but then I cannot change it. So I guess it is the ‘save’ function:
function save_pdf_link(){
global $post;
if (defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE){ return $post->ID; }
update_post_meta($post->ID, “link”, $_POST[“link”]);
}
My custom type it is called ‘events’. Any idea to make it work?
Thank You
hi mate, i have a custom post type , listing, i cannot add the above php code to it to display the PDF, when i add this code to functions.php then it gets added for my Posts but not my listings .
cheers
Hi, since upgrading to WP3.5 this has disappeared from my edit posts/pages, so I can no longer add/edit PDF’s. Can anyone help?
Thanks, Craig
RESOLVED!!! the snippet missing the “php”
sorry for my english:
you can integrate this code for a page? what are the changes to be done?
I did a test back into an post but I do not work that the link is
not make the connection with my file help please!!
Great job! But if I try to add multiple Metabox I end up getting the same file for every Custom filed.. I tried making different functions like: function pdf2_ Any ideas?
your code breaks my site.
found it very useful,is there any way to display file name instead of just PDF File:?
tnx
just replace
$pdf[]= $file->guid
with
$pdf[]= $file->post_title
This is a fantastic post, extremely useful. I am trying to adapt it to enable multiple PDF’s to be selected and then output but struggling, what would be the best way to approach? Any advice appreciated, and thanks again for the article
Não testei, mas tenta inserir o atributo “multiple” no .
Hi Kevin,
Got in here upon a search and the pdf_link function came in handy to fix as a part of a custom theme and post types am working on.
Thank you, you saved me hours.
Kevin I found the solution :
Replace the following :
echo ‘SELECT pdf FILE’;
with this :
echo ”;
I know this is a very old post but the right solution would be to pass a empty value i guess!
Replace the following :
echo ‘SELECT pdf FILE’;
with this :
echo ‘SELECT pdf FILE’;
I can think of so many websites that would benefit from this!
How would I go about including this into an if statement? So if a pdf has been uploaded, show the link for example…
Could could just replace the pdf_file_url function with the following that would set things up for you. http://pastebin.com/KZmigAyF
Thankyou so much Kevin, just tested it and that works like a charm 🙂 amazing – will re-tweet
Cool glad to hear I could help.
link not working