Home / Integrations / Wordpress / Contact form 7 Dynamic Attachments using custom upload field

About The Moo Master

As today's world moves towards more cloud based solutions, it's not always easy to find an all-in-one solution to a business problem. We focus on business development. finding new, creative and innovative ways to use different IT systems to enhance and automate business processes. We're happy to share our knowledge and solutions we've come up with over the years to some of those unique IT/Business problems.

22 comments

  1. Dear Moo Master,

    I’ve being trying your method but does not seems to be working.

    I only get the [_dynamic_attachments] shortcode in return of the email..

    • Hi Jasmin,

      Just noticed there was an error in my original code – Please try using [_attachment_url] or update the code and try using [_dynamic_attachments] again.

      Best,
      MooMaster

  2. thanks for the code,

    But it does not work for me, I’ve done it step by step and it does not work.

    I am using:

    [_attachment_url]
    [_dynamic_attachments]
    [Dynamic_attachments]

    and it does not work.

    • Hi Marcos,

      Do you receive any error when testing either [_attachment_url] or [_dynamic_attachments] ?

      • It does not show any error, the message that arrives by email does not have the link of the complete file. Only displays the [_dynamic_attachments] tag without the link.

        Here’s what I’m using:

        Plugin configured: Just Custom Fields

        Functions.php:

        Add_filter (‘wpcf7_special_mail_tags’, ‘dynamic_attachments’, 20, 3);
        Function dynamic_attachments ($ output, $ name, $ html)
        {
           $ Name = preg_replace (‘/^wpcf7\./’, ‘_’, $ name);
           If (‘_dynamic_attachments’ == $ name) {
                $ Url = explode (‘?’, ‘Http: //’.$_SERVER [“HTTP_HOST”]. $ _SERVER [“REQUEST_URI”]);
                $ ID = url_to_postid ($ url [0]);
                $ Attachment_id = get_post_meta ($ ID, ‘_acf_uploadfield’, true);
                $ Output = wp_get_attachment_url ($ attachment_id);
           }
           Return $ output;
        }

        ——————————————–
        Form

        Append purchase invoice (required)
            [File * UploadResume limit: 10000000 filetypes: jpg | pdf | word | png | gif | jpeg]

        E-mail:

        From: [your-name]
        Subject: [cf7-counter-rma]

        Message body:
        [Your-message]

        Serial number: [serie-755]
        Purchase Invoice: [UploadResume]
        [_dynamic_attachments]

        • Hi Marcos,

          Sorry for my very late reply on this one – I’ve noticed since the latest version of CF7 there was a change.

          Please update to the new version of my code and it should work now.

          In particular, the following lines changed
          $submission = WPCF7_Submission::get_instance();
          $url = $submission->get_meta( ‘url’ );
          $ID = url_to_postid($url);

          Best regards,
          MooMaster

  3. Dear Moo Master,

    Still cant get it to work.

    I have the same output even after updating the code

    And when I use the second option i just get this: “/” without quotes.

    Kind Regards

  4. Thank you so much Moo Master !!

    That did the trick.

    Regards

  5. Jacques Choquette

    Is there any way to get the uploaded file to be included as an attachement to the email instead of just a link to the file?

    I want the physical file to go out with the email not just a link

    any help on this would be greatly appreciated.

    • Hi Jacques,

      Please try the code in the second example
      “OPTION 2: Attachment relative root path”
      And then place the shortcode into the attachment field (Make sure you’re using HTML as the content type for the CF7 Email)

      Best regards,
      MooMaster

  6. Jacques Choquette

    Thanks for the quick response but this does not appear to be working

    I think it may have to do with the fact that contact form 7 docs here state the following
    https://contactform7.com/file-uploading-and-attachment/

    When the path is not an absolute path, it will be treated as a relative path to wp-content directory.

    So it’s expecting

    uploads/test-attachement.pdf

    but this function is outputting

    /wp-content/uploads/test-attachement.pdf

    I think to get this to work the /wp-content/ needs to be removed from the output

    Can you make a suggestion on how to modify that happy to send beer money in exchange for your assistance with this.

    • Jacques Choquette

      Ok I figured out how to modify the output string like so

      $output = ‘uploads/’ . rawurlencode( basename( $parsed[ ‘path’ ] ) );

      so now the short code is outputting

      uploads/test-attachement.pdf

      I put [_dynamic_attachments] in the File Attachements field and have use HTML content type checked but it’s still not working.

      If I had code
      uploads/test-attachement.pdf in the Files attachement box it works. It seems like the short code is not running in the file attachement area.

    • Hi Jacques,

      Modified version of some similar code i’m using. Please try out the below.

      https://pastebin.com/R7swidHD

      If it works, feel free to send beer to 1FfLqTnYABc6K3Cy75eKD5PCEbyqU7pK3d 🙂

      Best regards,
      MooMaster

      • Jacques Choquette

        Thanks but still not working

        I tried both [moometric_dynamic_attachments] and [moometric_dynamic_attachments] in the file attachements dialog box of the but no luck.

        Also not sure how to send funds to 1FfLqTnYABc6K3Cy75eKD5PCEbyqU7pK3d what is that reference number?

        Jacques Choquette
        [email protected]

  7. Thank You so very much for your code and assistance. I have been trying for days to get a dynamic attachment (woocommerce product thumbnail image) to attach to a CF7 form email submission and nothing I have tried to date worked.

    You are a genious!! 🙂

  8. Hi,

    I have setup woocommerce and have displayed the product enquiry button on the cart page (the products i have got here are from “add to cart” button). The problem i am having is to get the dynamic product name in to the ContactForm7 form.

    can you please guide me if there is a way for having the dynamically included product name in the contact form 7.

    Thanks.

  9. Hi MooMaster,

    Thanks for the reply, actually i am already using it on the products pages and it is working Great (y).

    But, currently i am working on the CART page, this page has for e.g. 3 products. i have setup a popup form(ContactForm7) with everything in it, but the only thing i need is, all the products in the cart page into the Product Enquiry form.

    Can you please help me out with this.

    Iffi

  10. Thank you ! Both options work very well, even with the ACF plugin! Thank you very much !

Leave a Reply

Your email address will not be published. Required fields are marked *