Skip Navigation

[Resolved] Can I get the file extension of an uploaded file for conditional logic?

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 1 voice.

Last updated by Minesh 4 weeks, 1 day ago.

Assisted by: Minesh.

Author
Posts
#2791045

I have a CPT Documents where a file can be uploaded (pdf, xslx, jpg, ...).
I would like to display the proper file extension icon in the View.
Can I get somehow get the file extension from an uploaded file so I can use it in a condition? Or can I only test against the whole file upload path?

#2791118

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You will required to add the custom shortocde to get the file extension. You should try to add the following custom shortcode to "Custom Code" section offred by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

add_shortcode('get_file_extension', 'func_get_file_extension');
function func_get_file_extension($atts, $content = ''){
    // Attributes
    extract( shortcode_atts(
        array(
            'option' => 'extension',
            'url'=>'',
        ), $atts )
    );
    $path_parts = pathinfo($atts['url']);
    $res = '';
    if($path_parts[$option]){
        $res = $path_parts[$option];
    }
    return $res;
}

Then to get the custom field file extension you can use it as:

[get_file_extension url="[types field='file' output='raw'][/types]"]

Where:
- Repalce "file" with your oritinal custom field field.

And to check the extension - you can use the [wpv-condtional] shortcode:
=> https://toolset.com/documentation/legacy-features/views-plugin/using-shortcodes-in-conditions/#checking-custom-shortcodes

So, in your case it would be:

Note:
Before using a custom shortcode inside a conditional, you need to register it. To do so, visit the Toolset -> Settings page and click the Front-end Content tab. There, simply add your shortcode "get_file_extension" (without quotes) to the Third-party shortcode arguments section.

For instance:

[wpv-conditional if="( '[get_file_extension url="[types field='file' output='raw'][/types]"]' eq 'pdf' )"] 
PDF file icon display here
[/wpv-conditional]
#2791160

I have
- pasted the snippet in the functions.php of the theme file.
- added the custom snippet via Toolset > Settings

But when I use [get_file_extension url="[types field='document-linked-files' output='raw'][/types]"]
It just displays: [get_file_extension url="hidden link"]
so the shortcode is not working.

#2791162

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

That is strange. You can make sure the custom code snippet where you added the code is active?

#2791171

I found the error in your example, those needed to be the same fuctions.

add_shortcode('get_file_extension', 'func_get_file_extension');
function get_mime_type_func($atts, $content = '')

#2791173

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ahh - while making some changes I must have forget to replace.

I've corrected that with the original reply:
- https://toolset.com/forums/topic/can-i-get-the-file-extension-of-an-uploaded-file-for-conditional-logic/#post-2791118