Contact Form 7 - File Upload Change File Name

Hello,

I have a contact form made in “Contact Form 7” in Wordpress. It has a file attach option, which works fine. I want to know if it is possible to rename the file that has been attached based on the input from another field?

There is a section for users to upload the logo for a sponsor at their event. Above the upload there is a text field for the sponsors name. I want the image upload to be renamed to the value of the text field.

This should be a simple tweak shouldn’t it? I need some advise and guidance.

Many thanks in advance.

Kind Regards

Ash

1 Like

Hi you have to ask the plugin author about this

You ever find a solution to this? I have the same need.

-Tim

Hi,

Yes I did find a solution. Unfortunately it required editing the CF7 plugin code, which is usually a big no no. So after very update I have to re-introduce the same edit. But it’s a simple fix for something that still works for the time being.

The file you need to mod is in “modules” folder, and it is the “file.php” file. The lines refering to the variable $filename is what you need to be looking at.

Hope that helps and points you in the right direction.

1 Like

Hi Underdog.
Do you think you might be able to send me your file.php file? I need to rename files in a specific format using two or three fields entered by the user, and could do with seeing a piece of code that already works.
I’ve set up a temporary mail account at. Would be really appreciated.

DM me a contact email so I can send.

Hi Underdog,
Iam also stuck with the same issue.can you send me your file.php

Hi, DM me your details.

Iam also stuck with the same issue.can you send me your file.php

I’m having the same problem. Can you send me your file?

Hi AnnyChagas,

DM me your contact details.

Hi, @Underdog89.

Thanks for your reply :smile:

I’m very new to Envato forum, and I don’t know how to send direct messages thought the forum. So I sent you an e-mail thought your themeforest profile.

Hi! great! Is it just what I was looking for, could you please send it to me?

Thank you very much in advance!!

I was able to pre-edit the filename, but not able to get the filename from another field in the form.

@Underdog89 not sure how to DM you. Can you kindly share the file on the forum or any other means. Greatly appreciated.

The code is too long to copy and paste the whole file.

  1. Open the file.php from CF7 plugin.

  2. Go to the wpcf7_file_validation_filter() function.

  3. Around line 167 replace with the following code.

    $tag_name=$tag->name;
    if (strpos($tag_name,‘sponsor-logo’) !== false) {
    $temp = explode(".", $file[‘name’]);
    $tag=explode(“sponsor-logo”,tag_name); if(_POST[‘sponsor-name’.$tag[1]]!=’’)
    filename=_POST[‘your-real-name’].’-’.$_POST[‘sponsor-name’.$tag[1]].’.’ . end($temp);
    else
    $filename=$file[‘name’];
    }
    else
    {
    $filename= $file[‘name’];
    }
    //$filename= $file[‘name’];
    $filename = wpcf7_canonicalize( $filename, ‘as-is’ );
    $filename = sanitize_file_name( $filename );
    $filename = wpcf7_antiscript_file_name( $filename );
    $filename = wp_unique_filename( $uploads_dir, $filename );

Please note, the $_POST variables should match the fields that you want to rename your file to. If you don’t have a basic understanding of PHP then leave this alone. Also, understand that if you update the plugin this change will be lost.

This is only a quick fix and should not really be looked at as a long term solution.

How do we do that on 02/2020 ??

Please help!

Hi,

I don’t need your help. You can see from above I have the solution already. I have made my own custom hook.

Thank you.

I got editing the plugin to work

image

but my attempt at a filter failed any ideas?

Would you mind sharing your filter example?