Contact Form 7 update success message - wordpress

I am trying to customise the success message of the Contact Form 7 plugin in WordPress.
I understand that it could be done using javascript, but I don't have this option. That's because I've created a custom coupon code on form submission, by using this - add_action('wpcf7_mail_sent', 'dbo_generate_coupon');.
I need to display that coupon to the user after the form is sent.
I don't want to use an alert. I need to alter the standard "Thank you for your message. It has been sent." response.
I am confident I can retrieve the cookie ok, but the first ( simple? ) step is getting control of the success message. Here I'm out of my depth.
I've tried code like
return apply_filters('wpcf7_messages','dbo_update_form_messages',10,1);
and
add_action('wpcf7_before_send_mail', 'action_wpcf7_mail_sent', 10, 1);
add_action('wpcf7_mail_sent', 'action_wpcf7_mail_sent', 10, 1);
Clearly I need help.
Thanks in advance.

With a little more digging, I found the answer on Stack Overflow
I updated the code from lukeseager under the heading Send the email to a dynamic recipient
This is my working startpoint;
function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
$dynamic_email = ''; // get your email address...
$properties = $contact_form->get_properties();
$properties['messages']['mail_sent_ok'] = 'custom message';
$contact_form->set_properties($properties);
return $contact_form;
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );

Related

Invalid JSON error when using var_dump or echo in functions.php

i have a customized Logic for my CF7 Form with the Hook before_mail_sent, whoever when i try to dump my Form Data like below i get this error in the Console on submit:
{code: 'invalid_json', message: 'The response is not a valid JSON response.'}
The Mail is sent but missing some data, thats why i want to print for debugging. A rollback to previous CF7 Version where it worked changed nothing.
2 weeks ago i was debugging the code and all was working fine. Im a bit stuck with the error message, hope somebody can help and explain.
Heres my code in functions.php:
add_action('wpcf7_mail_sent', 'ftw_veranstaltungsForm_afterSent' );
function ftw_veranstaltungsForm_afterSent( $contact_form ){
// to get form id
$form_id = $contact_form->id();
// Formular daten -> Assoziatives Array
$submission = WPCF7_Submission::get_instance();
$posted_data = $submission->get_posted_data();
var_dump($posted_data);
die;
}
Thanks in advance :)
It seems the action is being called in the middle of AJAX request and var_dump() messes up the JSON response.
you need to replace
var_dump($posted_data);
with
error_log( print_r( $posted_data, true ) );
and remove die();
and check the logged data in debug.log file.
https://wordpress.org/support/article/debugging-in-wordpress/

Contact Form 7 in Wordpress: How to set up for people to review what they have typed before sending?

I am using Contact Form 7 in wordpress. The form is simply just to type viewer's email address + Phone number and click Send!
How to setup for people to review their information one more time before sending?
Thank you.
You should use wpcf7_before_send_mail action hook. You should read documentation
function action_wpcf7_before_send_mail( $contact_form ) {
// do something
};
add_action( 'wpcf7_before_send_mail', 'action_wpcf7_before_send_mail', 10, 1 );

Advice on using wordpress contact form 7 hook

I have written the following codes:
// define the wpcf7_mail_sent callback
function action_wpcf7_mail_sent( $contact_form ) {
$temp1246 = get_post_meta( 1246, $key = 'ID', TRUE);
$temp1246 = intval($temp1246) + 1;
update_post_meta( 1246, 'ID', $temp1246 );
};
// add the action
add_action( 'wpcf7_mail_sent', 'action_wpcf7_mail_sent', 10, 1 );
What the code does is that it will increment a page's custom value by 1 every time a contact form is submitted. But the problem I have now is that I only want a specific contact form to trigger this code when it is sent, not all the contact forms (I have a few contact forms in the site powered by CF7).
May I know how can I adjust the codes above so that it is only triggered when the contact form (id = '1000') was sent for example?
Appreciate if anyone can assist, thank you!
Just found a solution that I can use the ID properties to find out which contact form I use:
if($contact_form -> id() == 1000){
}

Prevent Specific Email Registration Wordpress.org

So I'm looking for a way to prevent a specific email from registering an account on my website. It's a wordpress.org site.
I tried the Ban Hammer plugin, but it won't work.
I'm not looking for Comments, but for the site proper. Like a code I can put in functions.php or someplace and when this specific email is used to try and register an account on my site, to get an error.
Not an entire email domain, for example, #gmail.com. But a specific email, for example, stack#gmail.com.
Anyone knows how to do that?
EDIT: I found this tutorial here: http://www.davidtiong.com/block-spam-registrations-on-wordpress/
I tried adding this in Functions.php file right above the last ?> at the very bottom of the file:
function dtwd_blocked_emails($user_email) {
$dtwd_blocked_list = array("slojehupri#thrma.com", );
$user_email_split = explode('#', $user_email); $user_email_domain = $user_email_split[1];
if (in_array($user_email_domain, $dtwd_blocked_list)) {
//Return 1, for detection
return 1;
} else {
//Return 0 for no detection
return 0;
}
}
And I also added this in register.php of my theme:
elseif ( dtwd_blocked_emails( $user_email ) == 1) {
$errors->add( 'blocked_email', __( '<strong>ERROR</strong>: This email is not allowed.' ) );
}
And I added the same code in login.php of my theme.
And then I tried registering an account with this email (which should be blocked now): slojehupri#thrma.com
The site allowed me to register, and it allowed me to login. The email should've been blocked now and return an error when I try to register and/or login with it.
I'm not really sure how that function is supposed to work (it's not even hooked into anything...). I haven't tested this, but it sounds as simple as validating the email when the registration_errors filter hook is run. From the Codex:
The registration_errors filter hook filters the errors encountered when a new user is being registered. If any errors are present in $errors, this will abort the user's registration.
This sounds exactly like what you want to do (abort registration if the user email is in your blacklist). Again, this hasn't been tested, but I'd try something like the following in functions.php:
function so_32767928_blacklisted_user( $errors, $sanitized_user_login, $user_email ) {
// One or more blacklisted emails to validate against
$blacklist = array( 'slojehupri#thrma.com', );
// If the user trying to register is in the blacklist, add an error message
if ( in_array( $user_email, $blacklist ) ) {
$errors->add( 'blacklist_error', '<strong>ERROR</strong>: This email is not allowed to register on this site.' );
}
// Always return $errors, even if there are none
return $errors;
}
add_filter( 'registration_errors', 'so_32767928_blacklisted_user', 10, 3 );

check if user email exists, and if not, stop the submit of the ninja form

I've one form built with ninja forms, and I use ajax to send it.
I need to check if the email introduced already exists in database (user_email), and if it exists properly, I send the form properly, but if it doesn't exist, the form isn't submitted, and I need to give the user the message like "email does not exist".
The form is a survey to be completed by a registered user, who gives us a feedback about our services, but the survey is located in a page where the user can send its opinion without needed to be logged.
I'm investigating, and at the moment I have:
function example_disable_saving_subs( $save, $form_id ) {
global $ninja_forms_processing;
$form_id = $ninja_forms_processing->get_form_ID();
$email = ninja_forms_get_field_by_id( 18 );
//cuestionario feedback profesor sobre creación de un curso
if($form_id == 3){
if( !email_exists( $email )) {
$save = false;
$ninja_forms_processing->add_error('email_no_existe', 'El email no existe');
}
}
return $save;
}
add_filter( 'ninja_forms_save_submission', 'example_disable_saving_subs', 2, 10 );
But I pick up the field $email without value introduced...In addition, I don't know the way to give the user the message "email does not exists".
As you see, I chose the filter ninja_forms_save_submission. Maybe this is not the correct filter.
I hope your valious help.
Thanks in advance, Daniel
thanks for your help #Renato , I give you +1 :)
It's true that I can do it through the way you tell me, but I don't want to break the api of WordPress, that is, the way this cms uses javascript, php, etc etc...So, I wanted to do this through the API of ninja forms, which is the plugin I use for build this survey.
Finally, I solved it...it was my mistake, because I didn't use the correct filter...Investigating few more, there's another filter much more appropiate: ninja_forms_pre_process
Here is the code:
function add_change_ninja_forms_landing_page(){
add_action( 'ninja_forms_pre_process', 'handle_custom_ninja_forms' );
}
add_action( 'init', 'add_change_ninja_forms_landing_page' );
function handle_custom_ninja_forms(){
global $ninja_forms_processing;
$form_id = $ninja_forms_processing->get_form_ID();
//if it's my form of survey
if( $form_id == 3 ){
$email = $ninja_forms_processing->get_field_value( 18 ); //pick up the value of the email field
//use the native function of wordpress to check if there's a user with this email
//is anyone has this email, it does not exist
if( !email_exists( $email )) {
$ninja_forms_processing->add_error('email_no_existe', 'El email indicado no está registrado en nuestra base de datos'); //add_error stop the form and gives the error message
}
}
}
With the code above everything works fine! :)
Thanks!
Daniel,
I am not familiar with ninja_forms, but thinking of javascript, you can encapsulate your code to verify if users exists into an url and then, when making the ajax call, use it to verify...
If you can't change the ajax request, you can validate the field on it's blur event and disable the submit button untill it's marked as "successfull"
For you to create PHP files, and yet, use all Wordpress power and functionalities, you can simply include this file on the beggining of the file that will be called
require(wp-blog-header.php)

Resources