Skip Navigation

[Resolved] Show Full File Name rather than File URL in toolset blocks in CPT

This support ticket is created 2 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 2 replies, has 2 voices.

Last updated by sharlaC 2 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#2182021

Tell us what you are trying to do?
I'm using this code to change the toolset file field so that it shows the file name on the front end, rather than the URL on the front end:

//This allows us to grab the ID of an attachement by it's URL)
function pippin_get_image_id($image_url) {
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $attachment[0];
}

add_shortcode( 'my_file_name', 'my_file_name_func'); // Actually activate the shortcode
function my_file_name_func($atts) {
global $post; // So we can get the post meta later on

//define ShortCode attr prefix
$types = "wpcf-{$atts['types_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 = ''; // Setting up a variable to hold the links so we can return it later
$testurl = ''; //Creating URL Variable to Test Later

foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values
$arr = explode('/',$fileurl); // Split it up so that we can just grab the end part, the filename
$id = pippin_get_image_id($fileurl);
$title = get_the_title($id);
$content .= '<a href="'.$fileurl.'" title="'.$title.'" target="_blank">'.$title.'</a><br/>'; // Create whatever HTML and store it in the $content variable
$test_url = $fileurl;
}
}

if (!empty($test_url)) {

return $content; // Return the content as the shortcode value

} else {
return 'Sorry, we do not have this document available.'; //Polite message if no file found.
}

}

It works great, BUT it's not adding the ENTIRE file name. For example, on this page the file name is showing, but it's leaving the word "BROCHURE" off:

hidden link

But, if you look at the file itself, it's there: hidden link

How can I tweak this code in my functions.php file to show the entire file name?

Thanks
Is there any documentation that you are following?
No
Is there a similar example that we can see?
No
What is the link to your site?
hidden link

#2182245

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Sharla,

Thank you for getting in touch.

What needs to be done here is for you to return the basename of the file from the URL. I've modified your code to include this. Please let me know if it works now.

//This allows us to grab the ID of an attachement by it's URL)
function pippin_get_image_id($image_url) {
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $attachment[0];
}

add_shortcode( 'my_file_name', 'my_file_name_func'); // Actually activate the shortcode
function my_file_name_func($atts) {
global $post; // So we can get the post meta later on

//define ShortCode attr prefix
$types = "wpcf-{$atts['types_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 = ''; // Setting up a variable to hold the links so we can return it later
$testurl = ''; //Creating URL Variable to Test Later

foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values
$new_title = basename($fileurl); // Gets the URL filename 
$id = pippin_get_image_id($fileurl);
$title = get_the_title($id);
$content .= '<a href="'.$fileurl.'" title="'.$title.'" target="_blank">'.$new_title.'</a><br/>'; // Create whatever HTML and store it in the $content variable
$test_url = $fileurl;
}
}

if (!empty($test_url)) {

return $content; // Return the content as the shortcode value

} else {
return 'Sorry, we do not have this document available.'; //Polite message if no file found.
}

}

Note i've added the $new_title variable to the function to replace $arr.

Thanks,
Shane

#2183067

My issue is resolved now. Thank you so much!

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