Skip Navigation

[Resolved] Give titles to files uploaded in repeating field

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

Problem: I am using a repeating files field to store file uploads. I would like to output a list of links to each file, showing the filename only.

Solution:
Add this custom shortcode to your code snippets:

add_shortcode( 'my_file_name', 'my_file_name_func');
function my_file_name_func($atts)
{
  $urls = $atts['file_url'];
  $fileparts = explode('/', $urls);
  return end($fileparts);
}

Then use it in your template with the Types field shortcode:

[wpv-for-each field="wpcf-bestand"]
  <a href='[types field="bestand" output="raw"][/types]'>[my_file_name file_url='[types field="bestand" output="raw"][/types]']</a><br />
[/wpv-for-each]

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/#file

This support ticket is created 6 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 10 replies, has 2 voices.

Last updated by JosV9233 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1128134
toolset.jpg

What is the link to your site? hidden link
An example of the problem: hidden link (see attachment)

People can upload files through a repeating field of the type: file.

The output where you can see what was up[loaded looks like this:
[wpv-conditional if="(NOT(empty($(wpcf-bestand))))"][types field='bestand' link='true' title='Klik hier voor het bestand' separator=', '][/types][/wpv-conditional]

In this situation every file gets the title: Klik hier voor het bestand

How can I give meaningful titles to the uploaded files and output them in a column?

I have seen this: https://toolset.com/forums/topic/display-file-name-of-uploaded-file/
But it is too complicated. Can you help me on my way?

Regards,
Jos

#1128317

Hi, there isn't an easy way to associate a title with an existing repeating file field, because it's only designed to store one piece of information: the file URL. A different approach with Repeatable Field Groups, or RFGs, is more flexible. You can store both a file title and a file URL, using two separate fields. Then use a View to loop over each RFG and display the title as a link to the URL.

The RFG approach doesn't require any custom PHP code, but it may require a significant amount of work in wp-admin because it's not compatible with a repeating custom field. In other words, you would have to manually update each post that already contains a repeating file field. Then the conditional code you're currently using to display those repeating images must be replaced by a View of RFGs with a post relationship filter. Finally, any Forms you use to create this content on the front-end of the site must be updated and new RFG Forms must be added.

If that sounds like something you would like to explore, we have more information about creating and displaying RFGs available here: https://toolset.com/documentation/getting-started-with-toolset/creating-and-displaying-repeatable-field-groups/

#1128432

Hi Christian,
Thanks for your quick answer. I will look at he RFG approach. But before that I want to ask you this.
If it's designed to store the url of the file can I show the url then? And how should I do that?

#1128478

Of course, you can use different attributes in the Type Field shortcode to output the link using the file URL instead of the "title" attribute:

[types field='bestand' separator='<br />'][/types]

Notice that I also changed the "separator" value to add a line break after each item, since you said you want to show them in a column. Feel free to change it back to ", " if you prefer a comma-separated list.

#1128509

Hi Chris, that was easy ?, but it feels a little bit like a bad idea to have this url out in the open. Is there a way to omit the first part (=hidden link)? This will be equal for all uploaded files. If it s complicated do not bother, I am pretty happy already with your help and solution

hidden link

#1129158

You could use the wpv-for-each shortcode to loop over the repeated fields and output something more custom. I have a custom shortcode that can help you show just the file name without the full URL path. Add this code to your child theme's functions.php file, or create a new snippet in Toolset > Settings > Custom Code:

add_shortcode( 'my_file_name', 'my_file_name_func');
function my_file_name_func($atts)
{
  $urls = $atts['file_url'];
  $fileparts = explode('/', $urls);
  return end($fileparts);
}

Then use wpv-for-each to loop over the fields and output custom links:

[wpv-for-each field="wpcf-bestand"]
  <a href='[types field="bestand" output="raw"][/types]'>[my_file_name file_url='[types field="bestand" output="raw"][/types]']</a><br />
[/wpv-for-each]
#1129365
toolset2.jpg

Hi Christian,
Your snippet nicely shows the name of the file, only the link does not work. See the screenshot.

#1130006
Screen Shot 2018-10-18 at 10.42.52 AM.png

I'm not able to see the rekenen-met-coordinaten snippet, but the other 3 snippets I tested have links with accurate URLs:
hidden link
hidden link
hidden link

Were you able to resolve this issue on your own? If not, how can I see the problem?

#1130045

Because this site is life I changed the code back to the situation you see where the whole url is shown. The snippet you gave me

[wpv-for-each field="wpcf-bestand"]
<a href="[types field="bestand" output="raw"][/types]">[my_file_name file_url='[types field="bestand" output="raw"][/types]']</a>[/wpv-for-each]

shows only the last part of the url (what I wanted), but then the link does not work anymore.

I have changed it again to that situation so please check it out again.

The page was pending and is now published: hidden link

#1130076

It looks like the quotation marks are nested incorrectly in your snippet. Try exactly what I pasted before:

[wpv-for-each field="wpcf-bestand"]
  <a href='[types field="bestand" output="raw"][/types]'>[my_file_name file_url='[types field="bestand" output="raw"][/types]']</a><br />
[/wpv-for-each]
#1130095

My issue is resolved now. Thank you!