Target wpcf7mailsent of a specific contact 7 form

戏子无情 提交于 2019-12-01 11:27:39

As you can notice the event is not related to a specific form so you cannot make it triggered for a specific form. By the way you can add some logic inside the function to test which form is being used.

To do that you may use the event object. As you can read here :

User input data through the target contact form is passed to the event handler as the detail.inputs property of the event object. The data structure of detail.inputs is an array of objects, and each object has name and value properties.

and also

Available properties of the event object :

PROPERTY                      DESCRIPTION
detail.contactFormId          The ID of the contact form.
detail.pluginVersion          The version of Contact Form 7 plugin.
detail.contactFormLocale      The locale code of the contact form.
detail.unitTag                The unit-tag of the contact form.
detail.containerPostId        The ID of the post that the contact form is placed in.

You can add an if else statement in order to test a specific property you already specified in your form (an Id, a hidden input, etc)


In you case you may use the ID of your form 3112 by doing something like this :

document.addEventListener( 'wpcf7mailsent', function( event ) {
    if(event.detail.contactFormId == '3112') {
    //put your code here 
    }
}, false );

If you are not sure about the ID to use, you may add a console.log to see the content while submitting the form and you will get the ID and the other informations:

document.addEventListener( 'wpcf7mailsent', function( event ) {
    console.log(event.detail)
}, false );

Try This Code You can Differentiate form by their form id... And you can use more else if condition

   add_action( 'wp_footer', 'redirect_page' );

    function redirect_page()
    {
    <script type="text/javascript">
    document.addEventListener( 'wpcf7mailsent', function( event ) {
        if ( '111' == event.detail.contactFormId ) {
            location = 'your-url1';
        } else if ('222' == event.detail.contactFormId) {
            location = 'your-url2';
        }
    }, false );
</script>
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!