Skip Navigation

[Resolved] My custom shortcode stopped working (probably after an update)

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client reported that a custom shortcode to get the file size and the URL from a repeating file field wasn't working as expected.

Solution:
Shared some improvements to make the custom shortcode, loop through each instance of the file field properly.

Relevant Documentation:

This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 7 replies, has 2 voices.

Last updated by kristofG 5 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#1190994

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

[my-books-attachments]

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.

#1191000
#1191129

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

#1191940

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.

#1191941

sorry, closed this ticket too soon.

#1191981

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

#1192025

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?

#1192051

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

#1192200

Perfect, we now get a clean 200 HTTP code.
My issue is resolved now. Thank you!