-
AuthorPosts
-
September 12, 2018 at 4:43 pm #1009011
Hi,
I use the contact form of enfold and on forms pages, I need to get a variable that I put for Get in the url from Google Adwords and put it at the end of the subject of mail which is sent to the site owner with form content.
Is it possible?
Thanks,
Jordi
September 13, 2018 at 4:54 am #1009194Hey Empatica,
Thank you for using Enfold.
Are you trying to append some data on the autorespond email’s subject? I don’t think that’s possible because you can’t filter the “autoresponder_subject” once the email has been sent, at least in the theme’s case.
Best regards,
IsmaelSeptember 13, 2018 at 8:24 am #1009268Hi Ismael,
Oks! If it’s not possible to append data on the subject, would it be possible append data on the body of email? Or append data in any way for the site owner in order to know where the email comes from?
Thanks,
Jordi
September 13, 2018 at 9:16 am #1009282Hi,
I use the contact form of enfold and on forms pages, I need to get a variable that I put for Get in the url from Google Adwords
The question is: is the variable already set when the user comes to the contact form page?
If the page url of the contact form page contains the variable – i.e. like https://mywebsite.com/contact/?myvariable=4356754
you can use this code:
add_filter('avf_sc_contact_form_elements', 'avf_shortcode_sc_contact_form_elements', 10, 4); function avf_shortcode_sc_contact_form_elements($form_fields, $atts) { $data = ''; $googlevariable = 'myvariable'; if(!empty($_REQUEST[$googlevariable])) { $data = htmlspecialchars($_REQUEST[$googlevariable]); } if($data) { $form_fields[$googlevariable] = array('label' => '', 'type' => 'text', 'value' => $data); } return $form_fields; }
to dynamically generate a new form field based on the variable (must be a php variable). You can replace myvariable with any other variable name.
The e-mail which is then send out will contain the variable value. You can also use the filters I posted here: https://kriesi.at/support/topic/formating-the-email-sent-via-contact-form/#post-1009256 to modify the message. If you want to alter the subject you can use this code
add_filter('avf_form_subject','avia_change_mail_subject', 10, 3); function avia_change_mail_subject($subject, $new_post, $form_params) { $subject .= urldecode($new_post['4_1']); return $subject; }
and replace 4_1 with the field id of the field which contains the variable information.
Best regards,
PeterSeptember 13, 2018 at 6:51 pm #1009593Hi Peter,
Thanks for all, it works perfectly. The next step is: I have various forms with different numbers of field and the id field isn’t always the same. Is it possible that this hidden field would be the first one?
Thanks,
Jordi
September 14, 2018 at 3:17 am #1009760Hi,
You can replace the first filter with this.
add_filter('avf_sc_contact_form_elements', 'avf_shortcode_sc_contact_form_elements', 10, 4); function avf_shortcode_sc_contact_form_elements($form_fields, $atts) { $data = ''; $googlevariable = 'myvariable'; if(!empty($_REQUEST[$googlevariable])) { $data = htmlspecialchars($_REQUEST[$googlevariable]); } if($data) { $variable = array($googlevariable => array('label' => '', 'type' => 'text', 'value' => $data)); $form_fields = $variable + $form_fields; } return $form_fields; }
Best regards,
IsmaelSeptember 14, 2018 at 10:51 am #1009855Hi,
With this filter I don’t get the email . If you inspect the code, you can see the field appear the first, but I don’t get the email. If I put the first filter and the id manually it works correctly.
Can you know where to be the problem?
Thanks,
Jordi
- This reply was modified 6 years, 3 months ago by Empatica.
September 15, 2018 at 7:22 pm #1010218Hi,
Please try this code:
add_filter('avf_sc_contact_form_elements', 'avf_shortcode_sc_contact_form_elements', 10, 4); function avf_shortcode_sc_contact_form_elements($form_fields, $atts) { $data = ''; $googlevariable = 'myvariable'; if(!empty($_REQUEST[$googlevariable])) { $data = htmlspecialchars($_REQUEST[$googlevariable]); } if($data) { $form_fields = array($googlevariable => array('label' => '', 'type' => 'text', 'value' => $data )) + $form_fields; } return $form_fields; }
Best regards,
PeterSeptember 17, 2018 at 10:24 am #1010648Hi,
It doesn’t work. I can see the field above the form but the email is not sent. I try to put only text, without variable, to append on the subject and it doesn’t work either.
Sorry and thanks for all,
Jordi
September 17, 2018 at 1:55 pm #1010750Hi,
I now tested the code and obviously it’s not possible to make the hidden field the first one without breaking the code. I’d recommend to leave it at the end to use the field type hidden to hide it. Use this code for the form:add_filter('avf_sc_contact_form_elements', 'avf_shortcode_sc_contact_form_elements', 10, 4); function avf_shortcode_sc_contact_form_elements($form_fields, $atts) { $data = ''; $googlevariable = 'myvariable'; if(!empty($_REQUEST[$googlevariable])) { $data = htmlspecialchars($_REQUEST[$googlevariable]); } if($data) { $form_fields[$googlevariable] = array('label' => '', 'type' => 'hidden', 'value' => $data); } return $form_fields; }
Best regards,
PeterSeptember 17, 2018 at 4:50 pm #1010839Ok, thanks but the problem to leave it at the end is that I have various forms and all of them have different number of fields.
Then, if it’s at the end, I can’t control it by the id field at the second filter.
How can I do it?
Thanks,
Jordi
September 17, 2018 at 5:46 pm #1010866Hi!
You could use a prefix to mark your data – i.e. use this code
add_filter('avf_sc_contact_form_elements', 'avf_shortcode_sc_contact_form_elements', 10, 4); function avf_shortcode_sc_contact_form_elements($form_fields, $atts) { $data = ''; $googlevariable = 'myvariable'; $prefix = 'gv_'; if(!empty($_REQUEST[$googlevariable])) { $data = htmlspecialchars($_REQUEST[$googlevariable]); } if($data) { $form_fields[$googlevariable] = array('label' => '', 'type' => 'hidden', 'value' => $prefix.$data); } return $form_fields; }
– in the example I used the prefix ‘gv_’.
Then – in your filter code – you can use it to determine the right data field. I.e.:
add_filter('avf_form_message', 'avf_form_message_mod_new_field', 10, 3); function avf_form_message_mod_new_field($message, $new_post, $form_params) { $message = ''; $prefix = 'gv_'; foreach($new_post as $key => $data) { $pos = strpos($data, $prefix); if($pos !== false && $pos == 0) { $message = substr($data, strlen($prefix)); } } return $message; }
would replace the message with the content of your variable. If you want to append the content of the variable with some text to the message use:
$variabletext = 'My variable is: ' . substr($data, strlen($prefix)); $message .= $variabletext;
Regards,
PeterSeptember 19, 2018 at 5:52 pm #1011944Hi Dude,
Thanks for all. It’s working well.
Regards,
Jordi
September 19, 2018 at 6:27 pm #1011967Hi,
Great, glad we could help you :)
Best regards,
Peter -
AuthorPosts
- You must be logged in to reply to this topic.