wordpress-seo icon indicating copy to clipboard operation
wordpress-seo copied to clipboard

Quick Contact Form plugin conflict - form submits twice

Open fisicx opened this issue 11 years ago • 24 comments

I've developed a contact form plugin that works well and is reasonably popular. But when Yoast is activated the form always gets submitted twice.

I've tested with a whole range of other SEO plugins and they are all OK. It's just Yoast. I've tried everything to fix this but nothing blocks the double submission.

The contact form isn't anything complicated, it just assembles the message and uses wp_mail to send it off to the recipient. The page then reloads and a thank-you message is displayed.

This is the plugin: https://wordpress.org/plugins/quick-contact-form/

I'm sure it will be something simple but I can't find the bug.

fisicx avatar Aug 21 '15 17:08 fisicx

Hm, I remember another report with "submits twice" thing, but never got to the reason.

Could you give me a quick step by step how to reproduce in fastest way? I install your plugin, and?..

Rarst avatar Aug 21 '15 17:08 Rarst

Install the plugin Then go to Settings > Quick Contact. Enter your email address. Create a new page and add the shortcode [qcf] Send a test message. You will receive two copies of the message. I've got Yoast on the default settings (it might make a difference).

fisicx avatar Aug 21 '15 18:08 fisicx

PS: if you want a login to my test site send an email via this page: https://quick-plugins.com/quick-contact-form/

fisicx avatar Aug 21 '15 18:08 fisicx

If I follow code right you are POSTing form to current page and processing it in shortcode? Yeeeah, that would break things. Content is really poor place to have runtime code in.

The reason for doubling up is likely that we process the page content to get data for SEO stuff.

I would recommend to direct form submits to dedicated endpoint, WP has admin-post.php you could use.

Rarst avatar Aug 26 '15 09:08 Rarst

admin-post.php isn't going to work. The plugin allows for multiple forms on a page each one with their own settings. The form is set up, displayed, validated and processed within a loop that can't work if there is a dedicated endpoint.

All that is required is for $_POST to clear after form submission and before Yoast processes the page. But I can't find a way to do this.

fisicx avatar Aug 26 '15 15:08 fisicx

Tried using admin-post.php on a test plugin and it made no difference. Yoast works in the background which means the forms still get's submitted twice even if admin-post.php is called.

When my plugin form is submitted the $_POST handler isn't part of the current page. After validation and processing the page is reloaded with the thank you message.

fisicx avatar Aug 28 '15 07:08 fisicx

Could you please reduce this to simple proof of concept snippet that demonstrates the "double submit" issue?

Rarst avatar Aug 28 '15 07:08 Rarst

Not sure what you mean? I think I managed to find a fix for the contact form (by purging $_POST after submission) but I use the same structure on a number of plugins and one of them submits three times!

Register here: http://testblog.co.uk/event/new-registration-test/ and you will see what happens. If I deactivate yoast it works as it should. Using admin-post.php here won't work as there are multiple endpoints depending on how the user has set up the event, registration form and payment options.

fisicx avatar Aug 28 '15 07:08 fisicx

and you will see what happens

Could you please describe what actually happens, in technical terms?

My only reasonable guess so far is that the issue is with the way you process form after submit. If you claim that's not the case and the issue with actual POST submission running twice, so far I don't see how Yoast SEO could possibly cause it.

Rarst avatar Aug 28 '15 07:08 Rarst

  1. A function is called to set all the variables and display the form.
  2. On submission the function checks for $_POST['submit']
  3. The form is then validated.
  4. If it fails validation the page reloads with the error messages.
  5. If it passes validation the form is processed and the email sent.
  6. The page reloads and a thank you message displays.

loop > display > loop > validatepass > loop > process > loop > thank you

or

loop > display > loop > validatefail > loop > display > validatepass > process > loop > thank you

If Yoast is active the form gets send twice If Yoast is deactived the form gets sent once.

I did a load of testing last night and the form only gets processed once at the front end. If Yoast is doing something server side I can't block this happening.

fisicx avatar Aug 28 '15 07:08 fisicx

Cannot reproduce. I had put together a small logger snippet and I only capture single instance of wp_mail() call on form submission:

class R_Mailer {
    function Send() {
        error_log( wp_debug_backtrace_summary() );
    }
}

add_action( 'phpmailer_init', function( &$mailer ) {
    $mailer = new R_Mailer();
} );

If you log multiple calls in your setup please provide those backtraces.

Rarst avatar Aug 28 '15 10:08 Rarst

I think I've fixed the contact form issue but the event manager is much more complex. Once the form has been processed a confirmation message should be displayed. But when Yoast is activated the form gets submitted twice and visitors get an error message not a confirmation message.

Without Yoast activated: https://quick-plugins.com/event/2413/ With Youst activated: http://testblog.co.uk/event/new-registration-test/

You will see it working on the first but not on the second.

fisicx avatar Oct 10 '15 13:10 fisicx

Any progress?

fisicx avatar Oct 23 '15 06:10 fisicx

As above — I couldn't reproduce your issue and asked to provide backtraces. The code snippet above will generate and log backtraces if you need help creating them.

Demonstrating it on remote sites isn't helping it, I need to get to the exact technical issue of what our plugin is doing wrong (if anything).

Rarst avatar Oct 23 '15 06:10 Rarst

There is only a single instance of wp_mail(). I can see exactly what is happening:

  1. Registration form is submitted.
  2. Database updates and logs the registration.
  3. The page then reloads (if Yoast is activated) and the visitor gets an error message. If yoast isn't activated everything works and the visitor sees the thank you message. I added to the links so you can see what happens.

Where do you want me to put the snippet?

fisicx avatar Oct 23 '15 07:10 fisicx

Anywhere early enough during load would do (plugin, mu-plugin, etc).

Rarst avatar Oct 23 '15 07:10 Rarst

From the error log:

[23-Oct-2015 07:22:11 UTC] require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/aerin/single.php'), get_header, locate_template, load_template, require_once('/themes/aerin/header.php'), wp_head, do_action('wp_head'), call_user_func_array, WPSEO_Frontend->head, do_action('wpseo_head'), call_user_func_array, WPSEO_OpenGraph->opengraph, do_action('wpseo_opengraph'), call_user_func_array, WPSEO_OpenGraph->description, get_the_excerpt, apply_filters('get_the_excerpt'), call_user_func_array, wp_trim_excerpt, apply_filters('the_content'), call_user_func_array, get_event_content, qem_event_construct, qem_loop, qem_process_form, wp_mail, R_Mailer->Send

[23-Oct-2015 07:22:11 UTC] require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/aerin/single.php'), get_header, locate_template, load_template, require_once('/themes/aerin/header.php'), wp_head, do_action('wp_head'), call_user_func_array, WPSEO_Frontend->head, do_action('wpseo_head'), call_user_func_array, WPSEO_OpenGraph->opengraph, do_action('wpseo_opengraph'), call_user_func_array, WPSEO_OpenGraph->description, get_the_excerpt, apply_filters('get_the_excerpt'), call_user_func_array, wp_trim_excerpt, apply_filters('the_content'), call_user_func_array, get_event_content, qem_event_construct, qem_loop, qem_process_form, wp_mail, R_Mailer->Send

If Yoast is deactivated the message is: [23-Oct-2015 07:26:04 UTC] require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/aerin/single.php'), the_content, apply_filters('the_content'), call_user_func_array, get_event_content, qem_event_construct, qem_loop, qem_process_form, wp_mail, R_Mailer->Send

[23-Oct-2015 07:26:04 UTC] require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/aerin/single.php'), the_content, apply_filters('the_content'), call_user_func_array, get_event_content, qem_event_construct, qem_loop, qem_process_form, wp_mail, R_Mailer->Send

Two emails are sent: one notification to the site admin and one to the registrant to confirm their registration. Both messages arrive in my inbox

fisicx avatar Oct 23 '15 07:10 fisicx

According to your log there are two wp_mail() calls in either case and you expect two as well? Where are the doubled up ones called from?

The difference is likely from what I said way back — processing data in content/shortcodes just isn't a good technique.

Rarst avatar Oct 23 '15 07:10 Rarst

wp_mail() is called twice, once to send the notification of a new registration and a second time to send a message to the registrant. This is how it is supposed to work: I get an an email saying you have registered and you get a thank you email. The messages are different which is why wp_mail() is called twice.

The wp_mail() isn't the problem. The problem is the page content getting rendered twice. On the second page load the plugin checks the database and sees the registrant has already registered and displays an error message. This only happens when Yoast is activated.

fisicx avatar Oct 23 '15 08:10 fisicx

If you do a test registration on the two sites I linked to you will see the problem.

fisicx avatar Oct 23 '15 08:10 fisicx