Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
713 views
in Technique[技术] by (71.8m points)

wordpress - how to change form action url for contact form 7?

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?) *


See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...