Skip Navigation

[Resolved] Display the caption or file name of uploaded attachment files

This support ticket is created 5 years, 2 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 5 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1431227
Snap 2019-12-27 at 15.00.30, 810x301.png

Hi guys,

I am referring to an issue discussed on this thread and needed some clarification. https://toolset.com/forums/topic/display-the-file-name-of-uploaded-pdf-files-and-the-link-in-a-different-tab/

How do I actually modify the code of the block in order to display just the file name or the file caption? And ist there a way for the functions code to display the file name in case there is no file caption?

See the code of my Block in the attached screenshot.

Thanks in advance!

#1431911

Hello, the Types field shortcode does not provide a simple way to conditionally display the file title with the caption as a fallback, so a bit of custom code is necessary here. Assuming you want to output an unordered list of file names or captions (no links, just plain text), the contents in the Block editor should be exactly this:

<ul>[tssupp_repeating_file_names types_field="anhang" before_string="<li>" after_string="</li>"][tssupp_repeating_file_names]</ul>

Here is the custom code adjusted from the original thread. The adjustments were made to add flexibility and support the caption fallback. This code should be placed in your child theme's functions.php file, or in a new code snippet in Toolset > Settings > Custom Code:

add_shortcode( 'tssupp_repeating_file_names', 'wpml_hard_link'); // Actually activate the shortcode
function wpml_hard_link($atts) {
    global $post; // So we can get the post meta later on

    $atts = shortcode_atts( array(
      'file_url' => '',
      'types_field' => '',
      'before_string' => '',
      'after_string' => '',
    ), $atts );

    $url = "{$atts['file_url']}";
    $types = "wpcf-{$atts['types_field']}";
    $before_string = "{$atts['before_string']}";
    $after_string = "{$atts['after_string']}";
    $content = '';

    if ($types) { // if the types_field argument was provided

        $urls = get_post_meta($post->ID,$types); // let's get the (potentially multiple) values

        foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values
          $filepostid = pippin_get_image_id($fileurl);
          $filetitle = trim(get_the_title($filepostid));
          $filecaption = wp_get_attachment_caption($filepostid);
          $content .= $before_string;
          $content .= !empty($filetitle) ? $filetitle : $filecaption; // store the file title or fallback to file caption
          $content .= $after_string;
        }

    } else {  // Else we didn't use the fields_type argument, we just needed one URL we provided explicitly
        $filepostid = pippin_get_image_id($url);
        $filetitle = trim(get_the_title($filepostid));
        $filecaption = wp_get_attachment_caption($filepostid);
        $content .= $before_string;
        $content .= !empty($filetitle) ? $filetitle : $filecaption; // store the file title or fallback to file caption
        $content .= $after_string;
    }

    return $content; // Return the content and we're done!
}

// retrieves the attachment ID from the file URL
function pippin_get_image_id($image_url) {
    global $wpdb;
    $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
        return $attachment[0];
}

There are quite a few nuances here, so if the output isn't what you expect please tell me the exact HTML you would like to output from the repeating fields and I'll make some recommendations.

#1435619

Hi Christian,

thanks for the quick reply and a happy New Year to you and your team at Toolset.

Thanks also for the code, just one clarification: I actually the displayed file names to link to the respective files and open them in a new window. I'd be grateful if you would make that change in the code.

Thanks again!

Jannis

#1436635

Please show me the HTML you want to produce and I will be glad to make the necessary adjustments.

#1438453

Here is an example post of what I want to do:

hidden link

The links to the attachments should only display the file name, not the whole thing, and open the attached file in a new tab.

#1440969

Here is an update based on your example:

add_shortcode( 'tssupp_repeating_file_names', 'wpml_hard_link'); // Actually activate the shortcode
function wpml_hard_link($atts) {
    global $post; // So we can get the post meta later on

    $atts = shortcode_atts( array(
      'file_url' => '',
      'types_field' => '',
      'before_string' => '',
      'after_string' => '',
    ), $atts );

    $url = "{$atts['file_url']}";
    $types = "wpcf-{$atts['types_field']}";
    $before_string = "{$atts['before_string']}";
    $after_string = "{$atts['after_string']}";
    $content = '';

    if ($types) { // if the types_field argument was provided

        $urls = get_post_meta($post->ID,$types); // let's get the (potentially multiple) values

        foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values
          $filepostid = pippin_get_image_id($fileurl);
          $filetitle = trim(get_the_title($filepostid));
          $filecaption = wp_get_attachment_caption($filepostid);
          $content .= $before_string;
          $content .= '<a href="' . $fileurl . '" target="_blank">';
          $content .= !empty($filetitle) ? $filetitle : $filecaption; // store the file title or fallback to file caption
          $content .= '</a>';
          $content .= $after_string;
        }

    } else {  // Else we didn't use the fields_type argument, we just needed one URL we provided explicitly
        $filepostid = pippin_get_image_id($url);
        $filetitle = trim(get_the_title($filepostid));
        $filecaption = wp_get_attachment_caption($filepostid);
        $content .= $before_string;
          $content .= '<a href="' . $fileurl . '" target="_blank">';
        $content .= !empty($filetitle) ? $filetitle : $filecaption; // store the file title or fallback to file caption
        $content .= '</a>';
        $content .= $after_string;


    }

    return $content; // Return the content and we're done!

}

// retrieves the attachment ID from the file URL
function pippin_get_image_id($image_url) {
    global $wpdb;
    $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
        return $attachment[0];
}