4

I would like to add some PHP in my form created with Contact Form 7, a WordPress plugin. I've already tried this but it didn't work: Executing PHP Code in Contact Form 7 Textarea. Every time I insert the function, my website crashes.

I have a variable stored in a $_SESSION. I have put that variable in a hidden input and now I would like to take the value of the hidden input and place it in the contact form. How can I do this? Thanks!

Screenshot of the website and the hidden input

Screenshot of the back-end of the form

BenB
  • 2,747
  • 3
  • 29
  • 54
Bas
  • 98
  • 1
  • 1
  • 9
  • Sounds like you're looking for the [wpcf7_form_hidden_fields](http://hookr.io/filters/wpcf7_form_hidden_fields/) filter. – Danny Battison Mar 26 '20 at 00:28
  • What are you trying to add with php? Probably there are better ways than adding custom php in the middle of a form – BenB Mar 26 '20 at 00:38
  • @DannyBattison And how do I use that? – Bas Mar 26 '20 at 14:30
  • 1
    @BenB I'm trying to paste the URL from the previous page into the form. This way we know where the request is coming from. It's for a website of a car dealer. So people click on a car -> Click on 'More information' -> Go to the next page and fill out the form. I would like to paste the URL of the previous page into that form. Now I already have the URL stored in a $_SESSION var but now I need to paste that in a form. And I don't know how to do that. – Bas Mar 26 '20 at 14:32
  • Make your own form tag.https://contactform7.com/2015/01/10/adding-a-custom-form-tag/ – Howard E Mar 26 '20 at 14:42
  • @HowardE That's the thing. If I use code like that on the page, the website crashes. Check: https://imgur.com/zZv0qjf. It's Dutch but it says: A critical error has occurred on your website. And I also can't edit the functions.php. – Bas Mar 26 '20 at 20:58
  • Yes. Create your own custom form tag, where you use something like [session-var] and make that read the session variable and push it to a hidden form field. – Howard E Mar 26 '20 at 21:08

1 Answers1

13

I think this approach can work for you.

  1. Set a url param to the link. Example domain.com/more-info?previous-link=home-page
  2. In your form add this field

    [text* previous-link default:get default:post_meta "Previous link"]
    

You can see more examples in the docs

BenB
  • 2,747
  • 3
  • 29
  • 54
  • This worked! Although I bumped into another problem which was passing through multiple parameters in a URL. But using this topic I solved it: https://stackoverflow.com/questions/13102489/passing-multiple-variables-to-another-page-in-url. Thank you very much and also all the other people who helped :) – Bas Mar 27 '20 at 13:20