Reputation: 467
I have created on form by going into WordPress back end, then go into page, create new page, and on that page I have created on html form.
form code is giving in below.
<form action="http://example.com/wp-admin/admin-post_make_payment.php" method="post">
<input type="text" name="payment" class="form-control"/>
<input type="submit" name="Submit" class="form-control"/>
</form>
Now I want that when I submit form, then it will go and post data on functions.php file. Like I have make such type of action in functions.php file.
function do_payment() {
echo "<pre>"; print_r($_POST); echo "</pre>";
exit;
}
add_action('admin_post_make_payment', 'do_payment');
Now, can anyone help how to do that, it is giving error in above example.
Thanks
Upvotes: 1
Views: 14368
Reputation: 1144
First all, you can call from two differents ways the post ajax thought /wp-admin/admin-ajax.php or from /wp-admin/admin-post.php depending on the type of action you want to do.
in your case as you send a POST form you need to call "admin-post" so your code should look like:
<form action="http://example.com/wp-admin/admin-post.php" method="post">
<input type="hidden" name="action" value="make_payment">
<input type="text" name="payment">
<input type="submit" name="Submit">
</form>
Then you need to hook the action in the functions.php file. For that purpose yo have depending on the submit way (ajax or post). So for post admin_post_nopriv_{custom_action} or admin_post_{custom_action} and in ajax: wp_ajax_nopriv_{custom_action} & wp_ajax_{custom_action} The nopriv is going to fire when the user is not logged, and the other one when the user is logged. You can also point the two of them to the same function. So in your case you should do:
add_action( 'admin_post_nopriv_make_payment', 'make_payment_process' );
add_action( 'admin_post_make_payment', 'make_payment_process' );
function make_payment_process()
{
/* here you must put your code */
var_dump($_REQUEST['action']);
exit();
}
So I think that's all.
goodbye & goodcode.
Upvotes: 3
Reputation: 2080
You just simply do that.
<form action="http://example.com/wp-admin/admin-post.php" method="post">
<input type="hidden" name="action" value="make_payment" class="form-control"/>
<input type="text" name="payment" class="form-control"/>
<input type="submit" name="Submit" class="form-control"/>
</form>
It will hit on your given action in functions.php file.
The Second way is that. Suppose you have a form which is given in below in the Page.
<form method="post">
<input type="text" name="payment" class="form-control"/>
<input type="submit" name="make_payment" class="form-control"/>
</form>
You Just write the below code in your functions.php file.
if($_REQUEST['make_payment'] == 'make_payment') {
echo "<pre>"; print_r($_POST); echo "</pre>";
}
Thanks
Upvotes: 4