我同意其他答案,大多数情况下,最好使用插件来处理表单提交。然而,这条规则也有例外。
如果您发现自己需要自己处理表单提交,可以将数据提交到admin-post.php
. 以下是如何设置:
首先设置表单。
<!-- Submit the form to admin-post.php -->
<form action="<?php echo esc_url( admin_url(\'admin-post.php\') ); ?>" method="POST">
<!-- Your form fields go here -->
<!-- Add a hidden form field with the name "action" and a unique value that you can use to handle the form submission -->
<input type="hidden" name="action" value="my_simple_form">
</form>
现在,您将在
functions.php
文件您将使用表单中隐藏的操作字段中使用的值。
function my_handle_form_submit() {
// This is where you will control your form after it is submitted, you have access to $_POST here.
}
// Use your hidden "action" field value when adding the actions
add_action( \'admin_post_nopriv_my_simple_form\', \'my_handle_form_submit\' );
add_action( \'admin_post_my_simple_form\', \'my_handle_form_submit\' );
这里有一个链接,可以从codex获得更多信息-
https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)