您有多种方式来处理表单提交,其中之一是post操作。
但首先需要创建适当的表单属性和标记,以便它正常工作,所以它应该是这样的。
HTML
<form action="<?= esc_url(admin_url(\'admin-post.php\')); ?>" method="post">
<!-- this will "indicte" to the hook that will handle the submition -->
<input type="hidden" name="action" value="action_my_hook_name">
<!-- if needed you can add a nonce for security -->
<input type="hidden" name="security" value="<?= wp_create_nonce(\'my_form_nonce\'); ?>">
<input type="text" name="first_name" />
<input type="text" name="last_name" />
<input type="submit" value="Submit">
</form>
PHP(在functions.PHP中)
add_action(\'admin_post_action_my_hook_name\', \'action_my_hook_name\'); // logged in users
add_action(\'admin_post_nopriv_action_my_hook_name\', \'action_my_hook_name\'); // not logged in users
function action_my_hook_name () {
// checking if nonce was submited and is valid, if not valid will exit
check_ajax_referer(\'bt_site_nonce\', \'security\');
// handle form submition
}