我会在插件文件中使用init hook来注册自定义函数
add_action( \'init\', \'custom_proccess_form\' );
然后在自定义函数中检查表单输入,您提到过创建帖子
function custom_proccess_form() {
$wp_error = true; // report errors or not
$nonce = $_POST[\'_wpnonce\'];
if( isset($_POST[\'insert_post\']) && wp_verify_nonce($nonce, \'my-nonce-name\') ) {
$post = array(
\'post_title\' => $_POST[\'new_post_title\'],
\'post_content\' => $_POST[\'new_post_content\'],
);
// form actions
$post_id = wp_insert_post( $post, $wp_error );
// now you can use $post_id within add_post_meta or update_post_meta
// redirect at the end to some url
wp_redirect(\'/?success=true\');
}
}
表单本身可以如下所示
<form method="POST">
<?php wp_nonce_field(\'my-nonce-name\'); ?>
<input type="text" name="new_post_title">
<textarea name="new_post_content"></textarea>
<input type="hidden" name="insert_post" value="1">
<button type="submit">Submit!</button>
</form>