我为wordpress创建了一个插件,该插件可以通过简码向用户显示表单,我想提交表单,获取值并将其存储在激活插件时创建的定义表中。
现在我不知道该表格应该提交到哪里。我在管理面板中为插件定义了一个子菜单(并将表单操作设置为此子菜单项),以获取该值并将其存储在db中,但是只有那些登录后才能提交该表单。
答案 0 :(得分:2)
如果您的表单位于前端,则可以使用template_redirect
挂钩处理任何表单提交。如果您的表单位于后端,则可以使用admin_init
钩子
说,您的表单代码看起来像在前端
<form method="post">
<input type="text" name="input_1"/>
<input type="number" name="input_2"/>
<?php wp_nonce_field( 'name_of_your_nonce_action', 'name_of_your_nonce_field' ) ?>
<input type="submit" name="submit_form" value="Submit" />
</form>
现在在主题functions.php
文件中,您可以像
<?php
add_action( 'template_redirect', 'wp1213_handle_custom_form', 11 );
function wp1213_handle_custom_form() {
if( ! isset( $_POST['submit_form'] ) ) {
return;
}
if( ! wp_verify_nonce( $_POST['name_of_your_nonce_field'], 'name_of_your_nonce_action' ) ) {
return;
}
// Then you can handle all post data ($_POST) and save those data in db
.......
}