Skip Navigation

[Resolved] Downloadable Attachments from “File” Custom Field – display just filename

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

Problem:
Downloadable Attachments from "File" Custom Field - display just filename

Solution:
To display only filename from the custom file field, you need to add custom code that should return the only filename.

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/downloadable-attachments-from-file-custom-field/#post-1387815

Relevant Documentation:
Toolset offers the "Custom Code" section where you can add your custom PHP code.
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

This support ticket is created 5 years, 1 month 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by ethanB 5 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1387657

I have a custom field for File attachments (multiple files allowed). I'd like to display just the name of the file on the front end instead of the entire url starting with http

hidden link

I found the below threads which suggest a custom php script, and in 2016 it was noted as a feature request?

Has there been any progress on this feature?
If not is the php scrpit suggested here the best approach?

Thanks,
Ethan

https://toolset.com/forums/topic/getting-meta-data-like-filename-from-custom-fields/

https://toolset.com/forums/topic/display-file-name-of-uploaded-file/#post-349949

#1387815

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Has there been any progress on this feature?
=> no, this is feature is not added yet.

If not is the php scrpit suggested here the best approach?
=> Yes, thats the workaround we ask users to follow.

Toolset offers the "Custom Code" section where you can add your custom PHP code.
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

You should try to use the following code to display only fine name with the link

add_shortcode( 'display_file_name', 'func_display_file_name'); // Actually activate the shortcode
function func_display_file_name($atts) {
    global $post;  
   
    $url = "{$atts['file_url']}";
    $types = "wpcf-{$atts['field']}";
       
    if ($types) { // if the types_field argument was provided 
   
        $urls = get_post_meta($post->ID,$types); // let's get the (potentially multiple) values
 
        $content = '<ul>'; // Setting up a variable to hold the links so we can return it later
       
        foreach ($urls as $fileurl) {  
             
            $arr = explode('/',$fileurl);  
            $content .= '<li><a href="'.$fileurl.'">'.end($arr).'</a></li>';  
 
        }
        $content .= '</ul>';
           
        return $content; // Return the content as the shortcode value
       
    } else {  
            $arr = explode('/',$url);  
            return '<a href="'.$url.'">'.end($arr).'</a>';  
  }  
       
}

Now, you can call the above shortcode as:

[display_file_name  field="filefield"]

Where;
- Replace filefield with your original custom file field name.

Please feel free to modify the above code if required as per your requirement.

#1387839

Hi Minesh,
Thank you for your help. That worked!
Ethan