Skip Navigation

[Resolved] Display file name of uploaded file from user field

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

Problem:
How to display Filename of the uploaded file of a User Field?

Solution:
Solution here with screenshot:
https://toolset.com/forums/topic/display-file-name-of-uploaded-file-from-user-field/#post-575769

This support ticket is created 6 years, 7 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.

Our next available supporter will start replying to tickets in about 2.35 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

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

This topic contains 3 replies, has 2 voices.

Last updated by mandyS 6 years, 6 months ago.

Assisted by: Noman.

Author
Posts
#575708

I'm trying to do exactly the same as this: https://toolset.com/forums/topic/display-file-name-of-uploaded-file/#post-575699

I'm not sure how to adapt the following code with usermeta fields instead of fields and how should the shortcode be:

=>[types usermeta='service-document' link='true' title='[upload-file-title]' user_current='true' separator=''][/types] is it correct?
_________________________

//File title
add_shortcode('upload-file-title', 'upload_file_title_func');
function upload_file_title_func($atts){
global $wpdb;
extract( shortcode_atts( array(
'field' => 'wpcf-graph-file',
), $atts ) );
$file_array = get_post_meta(get_the_ID(), $field);
if(!empty($file_array))
{
$res = '';
foreach($file_array as $k => $v)
{
$id = url_to_postid($v);
if($id==0) {
$sql = "SELECT ID FROM $wpdb->posts WHERE guid= '$v'";
$id = $wpdb->get_var($sql);
}
$title = get_the_title($id);
$value = explode(',', $title);
$res .= '' . wp_get_attachment_link( $id, '', false, false, $value) . ', ';
}
$res .= '';
}
return $res;
}

_________________________

Thank you for your time and help,
Regards,
Mandy.

#575739

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Mandy,

Thank you for contacting Toolset Support. I would be happy to help you in this. I am checking this code further now for the User Fields and get back to you shortly with the results.

Thank you for waiting on this.

#575769

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Register Custom Shortcode.png

1. Please use this code and add it in your theme’s or child theme’s functions.php file:

add_shortcode( 'wpv_display_file_name', 'wpv_display_file_title_func');
function wpv_display_file_title_func($atts) {
 
    global $wpdb;
    $atts = shortcode_atts( array(
        'url' => '',
        'info' => '', // title, alt or id return
    ), $atts);
    $res = '';
    $url = $atts['url'];
    $attachment_id = $wpdb->get_var($wpdb->prepare(
        "SELECT ID FROM $wpdb->posts WHERE guid = %s", $url
    ));
    if($atts['info'] == 'title'){
        $res = get_the_title($attachment_id);
    }
    if($atts['info'] == 'alt'){
        $res = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
    }
    if($atts['info'] == 'id'){
        $res = $attachment_id;
    }
    return $res;
}

2. Please register shortcode first 'wpv_display_file_name' in Toolset >> Settings >> Front-end Content >> Third-party shortcode arguments -- screenshot attached.

3. Then use the shortcode in your View, I have tested this in Users View:

[wpv_display_file_name info="title" url="[types usermeta="custom-cover-image" url="true"][/types]"]

In above shortcode we have:
==> ‘info’ attribute, here we can define what is needed -- title, id or alt value.
==> ‘url’ attribute, here we will add Types shortcode that returns URL and we will get all info of this URL.

-- So that your required shortcode will be like:

[types usermeta='service-document' link='true' title='[wpv_display_file_name info="title" url="[types usermeta="service-document" url="true"][/types]"]' user_current='true' separator=''][/types]

I hope it helps, thank you

#577916

PERFECT!
thank you so much Noman!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.