Search code examples
phpwordpressadvanced-custom-fieldscontact-form-7

Retrieve wordpress image filename as it is loaded


I use CF7 to capture some user input and i want to add the functionality for the user to attach an image to that submission. I use ACF to configure my custom post types and meta data fields.

Documentation says that CF7 does not save images to the media folder by default but I'm finding that they are being saved. Some investigation suggests the ACF plugin causes this to happen. Now for me this works just fine as my aim is to save the image anyway BUT the uploaded filename (to the media library) gets 'tweeked' somewhere between CF7 capturing the file and it being saved into the media library.

I can grab the CF7 temp upload file (wp-content/uploads/wpcf7_uploads/0099184201/IMG_2780.jpeg) but this is then saved in the media library as wp-content/uploads/2025/02/1738853730-IMG_2780.jpeg.

As you can see the filename has been modified somehow (adding '/1738853730-' to it)

How do i get this media library filename / url ?

I have tried various event triggers on the CF7 side of things (before send mail etc) but all i can capture is what CF7 is doing with the file - which is saving it to a temp folder prior to sending an email and then deleting it (as expected).

I need the media library filename to be saved as a url on a CPT for later use.


Solution

  • For this I would suggest to capture the CF7 file temp path. From this you can exreact the original filename. Once you get the original name of the file, query for the attachment with the original filename in DB. Get the attachment url from attachment id and save it in the ACF field.

    add_action('wpcf7_mail_sent', function($contact_form) {
        $submission = WPCF7_Submission::get_instance();
        if (!$submission) {
            return;
        }
    
        $uploaded_files = $submission->uploaded_files();
        if (empty($uploaded_files)) {
            return;
        }
    
        foreach ($uploaded_files as $field_name => $file_path) {
            $file_name = basename($file_path); // Original filename (IMG_2780.jpeg)
            
            // Search for the uploaded file in the media library
            $args = [
                'post_type'      => 'attachment',
                'post_status'    => 'inherit',
                'posts_per_page' => 1,
                'meta_query'     => [
                    [
                        'key'     => '_wp_attached_file',
                        'value'   => $file_name,
                        'compare' => 'LIKE', // Since filename gets prefixed
                    ],
                ],
            ];
    
            $query = new WP_Query($args);
            if ($query->have_posts()) {
                $attachment_id = $query->posts[0]->ID;
                $media_url = wp_get_attachment_url($attachment_id);
    
                // Save to a custom post type (modify this based on your CPT setup)
                $post_id = YOUR_CPT_POST_ID; // Replace with your actual CPT post ID
                update_field('your_acf_field', $media_url, $post_id);
            }
        }
    });