简体   繁体   English

WordPress 表单操作 - functions.php 文件

[英]WordPress Form Action - functions.php file

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.我通过进入 WordPress 后端创建了表单,然后进入页面,创建新页面,并在该页面上创建了 html 表单。

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.现在我希望当我提交表单时,它会在functions.php 文件中发布数据。 Like I have make such type of action in functions.php file.就像我在functions.php文件中做了这样的动作。

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谢谢

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.它将在functions.php 文件中执行您给定的操作。

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.您只需在您的functions.php 文件中编写以下代码。

if($_REQUEST['make_payment'] == 'make_payment') { 
    echo "<pre>"; print_r($_POST); echo "</pre>";
}

Thanks谢谢

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.首先,您可以从两种不同的方式调用 post ajax 认为 /wp-admin/admin-ajax.php 或从 /wp-admin/admin-post.php 取决于您想要执行的操作类型。

in your case as you send a POST form you need to call "admin-post" so your code should look like:在您的情况下,当您发送 POST 表单时,您需要调用“admin-post”,因此您的代码应如下所示:

<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.然后你需要在functions.php文件中hook这个动作。 For that purpose yo have depending on the submit way (ajax or post).为此,您可以根据提交方式(ajax 或 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.因此,对于 post admin_post_nopriv_{custom_action} 或 admin_post_{custom_action} 以及在 ajax 中: wp_ajax_nopriv_{custom_action} & wp_ajax_{custom_action} nopriv将在用户未登录时触发,而在用户登录时触发另一个。 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.再见和好代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM