We have a view that loops over any attached pdfs to a post. A bit like an image gallery but with documents. It uses this shortcode
which comes from our functions.php (code proviced by Toolset)
function my_books_attachments_shortcode($atts) {
extract(shortcode_atts(array(
'attachment' => '',
), $atts ));
global $post, $wpdb;
$attachments[] = get_post_meta($post->ID, 'wpcf-book-pdf', false);
$attachments[] = get_post_meta($post->ID, 'wpcf-book-e-pub', false);
$attachments = array_filter($attachments);
if (!empty($attachments)) {
$out = '';
foreach ($attachments as $attachment) {
$attachment_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid = '%s';", $attachment));
$attachment_title = get_the_title($attachment_id);
$caption = get_the_excerpt($attachment_id);
$description = get_the_content($attachment_id);
//$alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
$out .= '<li>';
$out .= '<a href="' . $attachment[0] . '" title="' . $attachment_title . '" target="_blank" class="attachment-link">'.$attachment_title.'</a>';
$out .= formatSizeUnits(filesize( get_attached_file($attachment_id)));
$out .= '</li> ';
}
}
$out .= '';
return $out;
}
add_shortcode('my-books-attachments', 'my_books_attachments_shortcode');
function formatSizeUnits($bytes)
{
if ($bytes >= 1073741824)
{
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
}
elseif ($bytes >= 1048576)
{
$bytes = number_format($bytes / 1048576, 2) . ' MB';
}
elseif ($bytes >= 1024)
{
$bytes = number_format($bytes / 1024, 2) . ' kB';
}
elseif ($bytes > 1)
{
$bytes = $bytes . ' bytes';
}
elseif ($bytes == 1)
{
$bytes = $bytes . ' byte';
}
else
{
$bytes = '0 bytes';
}
return $bytes;
}
We have just noticed 2 issues
1/ the shortcode no longer loops and shows just the 1st attached pdf
2/ the file size is 0 bytes.
Hi Kristof,
Thank you for waiting.
I've performed a few tests and noticed that your shortcode's snippet needs some adjustment.
Please replace the following lines:
$attachments[] = get_post_meta($post->ID, 'wpcf-book-pdf', false);
$attachments[] = get_post_meta($post->ID, 'wpcf-book-e-pub', false);
$attachments = array_filter($attachments);
With:
$story_files = get_post_meta($post->ID, 'wpcf-book-pdf', false);
$book_pubs = get_post_meta($post->ID, 'wpcf-book-e-pub', false);
if(!empty($story_files)) {
foreach ($story_files as $story_file) {
$attachments[] = $story_file;
}
}
if(!empty($book_pubs)) {
foreach ($book_pubs as $book_pub) {
$attachments[] = $book_pub;
}
}
I hope this helps and let me know how it goes.
regards,
Waqar
Hi, I replace the old code with your new code and at first the href was always "h" which resulted in a 404. Then I noticed that I also had to add an "s" to
<a href="' . $attachments[0]
Now the href looks good, except, it is the same link/pdf for both links.
sorry, closed this ticket too soon.
Hi Kristof,
Thanks for the update.
The original code is using "$attachments[0]" value for the URL/link, which isn't correct.
You can replace:
$description = get_the_content($attachment_id);
With:
$description = get_the_content($attachment_id);
$attachment_url = get_the_permalink($attachment_id);
After that, you'll be able to use the "$attachment_url" value for creating the link:
$out .= '<a href="' . $attachment_url . '" title="' . $attachment_title . '" target="_blank" class="attachment-link">'.$attachment_title.'</a>';
regards,
Waqar
Hi Waqar,
I have edited the php code and now the link does open a pdf in a new tab, but indirectly. For some strange reason, it seems to open the attachment url first and then redirects to the actual pdf file. Although this seems to work, we would rather link to the pdf directly instead of that 301 Permanent Redirect. Is that possible?
Hi Kristof,
Glad it works and to use the attachment's file direct URL, you can use "wp_get_attachment_url" function instead.
( ref: https://codex.wordpress.org/Function_Reference/wp_get_attachment_url )
Replacing:
$attachment_url = get_the_permalink($attachment_id);
With:
$attachment_url = wp_get_attachment_url($attachment_id);
regards,
Waqar
Perfect, we now get a clean 200 HTTP code.
My issue is resolved now. Thank you!