Hello,
I have a field for uploading files as a user field, which is set to be repeatable. My view displays these file fields only for logged in users.
I have followed this topic: https://toolset.com/forums/topic/display-file-name-of-uploaded-file-from-user-field/, but instead of the file name it displays name of the page.
[wpv_display_file_name info="title" url="[types usermeta="dokument" url="true"][/types]"]
Hi Dave,
Thank you for waiting and I apologize for the delay.
During troubleshooting, I noticed that the custom shortcode that you're using has certain limitations and would show the page's title and not the media file's title, in the following cases:
1. If the user field has more than one values (repeating).
Or
2. If the user field has the file URL saved, which is different than the originally uploaded file.
For example, the actual file's URL is lien caché, but a resized thumbnail's URL is saved e.g. lien caché
To overcome the limitation of point 1, you can register a new shortcode that takes care of the repeating file fields.
As much as we would like to help, 1-1 custom code assistance such as this is not covered under the support that we provide
( https://toolset.com/toolset-support-policy/ ).
But for more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
I hope this helps!
regards,
Waqar
Hi Waqar,
finally I have decided that I will not complicate it, so I can use the filename like https://toolset.com/forums/topic/give-titles-to-files-uploaded-in-repeating-field/
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);
}
So could you, please, explain how the shortcode that will take care of the repeating file fields should look like? Thank you.
Hi Dave,
For a repeating user file field, you can update that shortcode to:
add_shortcode( 'get_usermeta_shortcode', 'get_usermeta_shortcode_func');
function get_usermeta_shortcode_func($atts)
{
$user_id = $atts['user_id'];
$separator = $atts['separator'];
$field = $atts['field'];
$user_meta = get_user_meta( $user_id, $field, false );
foreach ($user_meta as $value) {
$fileparts = explode('/', $value);
$user_meta_arr[] = end($fileparts);
}
if(!empty($user_meta_arr)) {
return implode($separator, $user_meta_arr);
}
}
After that, you'll be able to use the shortcode as:
[get_usermeta_shortcode user_id='[wpv-user field="ID"]' field='wpcf-field-slug' separator=', ']
Note: you'll replace "wpcf-field-slug" with the actual field's slug starting with "wpcf-" and ', ' with the actual separator that you'd like to use between the file names.
regards,
Waqar
Hi Waqar,
thank you for your help. I tried to use this code, but it shows only two files of three and the first one is even different, which is odd. Please see lien caché (you have to be logged in). The three documents on the top are the actual files displayed using [types usermeta]. My goal is to get these names as links like in attachment.
Hi Dave,
Thanks for writing back.
I've checked the link you've shared ( lien caché ) and the shortcode seems to be working as expected.
( screenshot: lien caché )
Please note that your view "Administrace - zobrazení dokumentů" ( lien caché ) is set to get the only users with role "Firma".
( screenshot: lien caché )
And in the user's list, there is just one user, with this role:
lien caché
( screenshot: lien caché )
If you'll check that user's profile, you'll see there are just two files added for the "wpcf-dokument" field, whose names are shown on the front-end:
lien caché
( screenshot: lien caché )
If you'd like to link those file names to the original files, you can update the following line from the last code snippet, from:
$user_meta_arr[] = end($fileparts);
To:
$user_meta_arr[] = '<a href="'.$value.'">'.end($fileparts).'</a>';
Note: I'd like to remind you again that If you're finding the custom code difficult to customize as per your requirement, it would be a good idea to hire a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
Hi Waqar,
it helped me a lot, I have exactly what I wanted, thank you. It works great now. I just changed the user_id='[wpv-user field="ID"]' to user_id='[wpv-current-user info='id']', so now it displays files of current logged in user.
Can you please remove the e-mail address from previous post? Thank you.
Hi Dave,
Thanks for the update and glad it works.
I've removed the email address from my last message and for a new question/concern, you're welcome to open a new ticket.
regards,
Waqar
My issue is resolved now. Thank you, Waqar!