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.
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.
Noman
Supporter
Languages:
English (English )
Timezone:
Asia/Karachi (GMT+05:00)
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
PERFECT!
thank you so much Noman!