15

I'm using Contact Form 7 in a wordpress site with multiple forms. I need to direct one form to a different form action url than the others.

I found the reply below for a previous thread but I'm not sure how to go about it. Can someone specify what exact code needs to be included in "additional settings" and what the code in functions.php would look like?

Thanks for your help!


reply from diff. thread, which I don't completely understand...

*Yes, you have to change the "action" attribute in the form using this Filter Hook wpcf7_form_action_url. (what would be the code?) You could add the hook into your theme's functions.php and then just process the form data in your ASP page.(code?) *


user1102824
  • 161
  • 1
  • 1
  • 7
  • 1
    Link to previous thread? What have you tried? SO won't code it for you though it can help you with the code you already are writing – FelipeAls Jan 06 '13 at 00:29
  • I apologize, I'm a designer working on a WP site that was set up before me. Seeing that contact form 7 is already installed and various forms set up, I added a new form and got email notification working. But I need the data for the new form sent to a different .asp than the default. After searching online, it seems this can be done by adding code to additional settings in CF7, add_filter(wpcf7_form_action_url, ________)and the function.php file? I've found countless php codes on how to redirect a url but nothing on php to change url of form action...apologies for being in the dark re: this.. – user1102824 Jan 06 '13 at 02:34
  • here's the link to prev. thread: http://stackoverflow.com/questions/7101614/contact-form-7-post-to-asp – user1102824 Jan 06 '13 at 02:39
  • That still doesn't explain what you've tried so far. The answer in the previous thread actually tells you everything you need to write the code required to make the change. As FelipeAls mentioned, we're here to help you learn and help you with code you yourself have developed. Not do your work for you. – maiorano84 Jan 06 '13 at 02:45
  • Apologies, I'm completely unfamiliar with php and I've read several tutorial type articles and a bit overwelmed by the terms, completely foreign to me. Any help or direction regarding step by step coding or where to start would be much appreciated. Thanks. – user1102824 Jan 06 '13 at 03:29

4 Answers4

41

Since you're not familiar with PHP code at all, I'll give you a bit of a crash course in coding within the Wordpress API.

First off, you need to know the difference between functions and variables. A variable is a single entity that is meant to represent an arbitrary value. The value can be anything. A number, somebody's name, or complex data.

A function is something that executes a series of actions to either send back - or return - a variable, or alter a given variable.

<?php
$a = 1; //Number
$b = 'b'; //String *note the quotes around it*
$c = my_function(); //Call to a function called my_function
echo $a; //1
echo $b; //b
echo $c; //oh, hello
function my_function()
{
    return 'oh, hello';
}
?>

Wordpress utilizes its own action and filter system loosely based on the Event-Driven Programming style.

What this means is that Wordpress is "listening" for a certain event to happen, and when it does, it executes a function attached to that event (also known as a callback). These are the "Actions" and "Filters". So what's the difference?

Actions are functions that do stuff
Filters are functions that return stuff

So how does this all fit in to your problem?

Contact Form 7 has its own filter that returns the URL of where information is to be sent by its forms.

So lets look at the basics of a Filter Hook

add_filter('hook_name', 'your_filter');
  1. add_filter is the function that tells Wordpress it needs to listen for a particular event.
  2. 'hook_name' is the event Wordpress is listening for.
  3. 'your_filter' is the function - or callback - that is called when the 'hook_name' event is fired.

The link to the previous thread states that the hook name you need to be using is 'wpcf7_form_action_url'. That means that all you have to do is make a call to add_filter, set the 'hook_name' to 'wpcf7_form_action_url', and then set 'your_filter' to the name of the function you'll be setting up as your callback.

Once that's done, you just need to define a function with a name that matches whatever you put in place of 'your_filter', and just make sure that it returns a URL to modify the form action.

Now here comes the problem: This is going to alter ALL of your forms. But first thing's first: See if you can get some working code going on your own. Just write your code in functions.php and let us know how it turns out.

UPDATE:

The fact that you were able to get it so quickly is wonderful, and shows the amount of research effort you're putting into this.

Put all of this in functions.php

add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');
function wpcf7_custom_form_action_url()
{
    return 'wheretopost.asp';
}

As mentioned before, that will affect ALL of your forms. If this is only supposed to affect a form on a given page, you can do something like this:

add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');
function wpcf7_custom_form_action_url($url)
{
    global $post;
    $id_to_change = 1;
    if($post->ID === $id_to_change)
        return 'wheretopost.asp';
    else
        return $url;
}

All you would need to do is change the value of $id_to_change to a number that represents the ID of the Post/Page you're trying to affect. So if - for example - you have an About Page that you would like to change the Action URL, you can find the ID number of your About Page in the Admin Dashboard (just go to the Page editor and look in your URL for the ID number) and change the 1 to whatever the ID number is.

Hope this helps you out, and best of luck to you.

maiorano84
  • 11,574
  • 3
  • 35
  • 48
  • 2
    I've purposely not given you the complete answer straight away. You need to read through this, come up with the correct add_filter call, and your callback function on your own. Once you do that, we can guide you through more. All the pieces are there for you. – maiorano84 Jan 06 '13 at 04:39
  • maiorano84, thanks so much for the reply. Yes, i did get to add_filter('wpcf7_form_action_url', 'formname'); but your explanation clarifies the logic. How to "make sure that it returns a URL to modify the form action" is where I'm completely lost. If this was outside of CF7/WP, I would just code
    in html but not sure what the php version of that would be? Again, I apologize for my lack of understanding. Thanks.
    – user1102824 Jan 06 '13 at 04:47
  • 1
    ok, i'll look into it some more tmrw as I feel I'm holding the pieces and completely blind. thanks so much. – user1102824 Jan 06 '13 at 04:54
  • I'll be around. I know you can do it. – maiorano84 Jan 06 '13 at 04:58
  • 1
    ok,came up with adding in CF7 "additional settings": add_filter('wpcf7_form_action_url', 'formname'); and in functions.php adding code: function formname() { return "http://whereToPost.asp"; } – user1102824 Jan 06 '13 at 21:37
  • @user1102824 I've provided an edit for you that should do what you need. I haven't tested it, but this should get you started in the right direction. Thank you for taking the time to research the problem, and I'll be honest: I'm thoroughly impressed with how quickly you arrived at the correct solution. Good luck to you. – maiorano84 Jan 06 '13 at 22:19
  • Wow. Thanks so much. I will test it on the server tmrw and post back. It seems I've got much to learn.. – user1102824 Jan 06 '13 at 22:34
  • 1
    I tested with add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url'); in the functions.php file and the form, once submit button is clicked, shows processing circle and no response. So, I put that code in the CF7 under 'additional settings' instead. That allows me to receive an email with the data but doesnt seem to talk with the external .asp. The .asp url itself shows an error when viewed in browser: "Sorry, there has been a system error, FromAddress Property cannot be blank." Not sure if code needs to be revised or if someth is wrong w the .asp itself, which would be IT issue. – user1102824 Jan 08 '13 at 00:34
  • Good on you for figuring out where to move the add_filter call. Since the .asp page is coming up, that mean the action is being called as intended. The error means that data isn't being processed by the .asp page correctly which is unrelated to Wordpress. Hope your IT guys are able to figure it out. – maiorano84 Jan 08 '13 at 00:42
  • ah, i mean the .asp doesn't come up through form submit but when i type the .asp url in browser the error message shows. – user1102824 Jan 08 '13 at 03:19
  • Well, typing in the URL won't send data to the form, so an error would be expected. Right-click the form and inspect the element. What does the form action say? – maiorano84 Jan 08 '13 at 03:47
  • inspecting element on submit button shows
    . (p1178 matching the page id#) i tried giving the function and permalink same name which didn't work..
    – user1102824 Jan 08 '13 at 15:59
  • ..im assuming either the php code or the.php file code is placed needs to change..? any suggestions/thoughts greatly appreciated. thanks. – user1102824 Jan 10 '13 at 15:49
  • Sorry, didn't see the other response. Apart from moving the add_filter call back to functions.php and inspecting the Form Action in your Web Inspector, I'm fresh out of ideas (I don't particularly use Contact Form 7). Did you use the first or second example? If you used the second, try using the first and seeing if all of your forms are affected. – maiorano84 Jan 10 '13 at 16:07
  • Sry for the delayed response-I tried both examples and placing the code in various places w no go.. contacted IT and they're trying to sort out. Thanks so much for your help. I feel learning php would be more helpful in long run than trying to customize themes w limited results. If there are any online/book resources you'd recommend, I'd much appreciate. Thanks again for your time. – user1102824 Jan 29 '13 at 02:43
  • @user1102824 Sorry we couldn't get it up and running this time around. If you have an interest in learning any kind of programming (PHP is just one of many flavors), my advice is to just sit down and play with it on your own. Hands-on learning is MUCH more effective than theory. Start with writing a Hello World script in PHP, then experiment with achieving the same result in as many different ways as you can. The [PHP Manual](http://php.net/manual/en/index.php) and [Wordpress Codex](http://codex.wordpress.org/) are your friends. Good luck. – maiorano84 Jan 29 '13 at 14:13
  • Cant we put condition over the form id? – Maha Dev Apr 24 '17 at 07:53
  • If you use this filter, you will lose features like validation. Moreover, for this filter to work, you must disable the JS from CF7 – kanlukasz Jul 22 '20 at 09:34
  • Is there any hook to change the form attribute "action" ? – Sudhanshu Dec 10 '20 at 20:04
4

Great answer @maiorano84 but I think you should check form ID instead of Post. Here is my version.

add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');
function wpcf7_custom_form_action_url($url)
{
    $wpcf7 = WPCF7_ContactForm::get_current();
    $wpcf7_id = $wpcf7->id();

    $form_id = 123;
    

    return $wpcf7_id == $form_id? '/action.php' : $url;
}

Another thing you might need to disable WPCF7 AJAX. That can be disabled by placing the following code in your theme functions.php

apply_filters( 'wpcf7_load_js', '__return_false' );
123
  • 2,169
  • 3
  • 11
  • 35
Abbas Arif
  • 372
  • 4
  • 16
  • Disable WPCF7 using apply_filters AJAX doesn't work for me. – José Carlos PHP May 05 '21 at 19:06
  • 1
    If the filter isn't working you can define in the wp-config.php ```define('WPCF7_LOAD_JS', false);``` – Abbas Arif May 05 '21 at 20:10
  • Yes I finally did it inside wp-config.php, however I tweaked some more because I didn't want to disable WPCF7_LOAD_JS for every form, just for one. It's working fine. – José Carlos PHP May 06 '21 at 09:34
  • If anyone comes across this: filter doesn't work because op didn't change `apply_filters` to `add_filter`. Also, you can use same `WPCF7_ContactForm::get_current()` logic in that filter to limit it to specifc forms (tested). Also worth noting that if no cf7 is present on page, website will crash as `$wpcf7` will be null. So, need to check for that with something like `$wpcf7_id = $wpcf7 ? $wpcf7->id() : 0;`. – Danyl Filatov Feb 23 '23 at 10:33
0

You can add actions after a successful submission like the documentation says

Adding a filter will work in the sense that it will change the action on the form but unfortunately it will also break the functionality of the plugin. If you add the filter like other answers suggest the form will keep the spinner state after submission.

You can make the form do something else on submit by using advanced settings such as:

on_submit: "alert('submit');"

more details about advanced settings here.

pcatre
  • 1,304
  • 2
  • 16
  • 24
  • Note: on_sent_ok and on_submit have been officially removed from Contact Form 7 5.0. You can use DOM events instead of these settings. – kanlukasz Sep 21 '19 at 13:08
0

According to @abbas-arif, his solution works great, but have a limitation. This solution change the form's action on all forms present in post with that ID. A better solution should be to use directly the form's ID. To get it, whit wordpress >5.2, you can use:

add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');
function wpcf7_custom_form_action_url($url)
{
    $cf7forms = WPCF7_ContactForm::get_current();
    $Form = $cf7forms -> id;
    
    
        switch($Form){
            case 1:
                return 'destination like salesforce url 1...';
            case 2:
                return 'destination like salesforce url 2...';
            case 3:
                return 'destination like salesforce url 3...';
            default:
                return $url;
            
        }

}
Aculine
  • 21
  • 1
  • 4