将操作值保留为空。给出submit
按钮名称为name=\'form_sub\'
. 然后将以下代码添加到functions.php
文件检查法典init
钩您可以创建一个“谢谢”页面,或一个确认页面,用户在成功提交后应前往该页面。
<?php
add_action(\'init\', \'form_submit\');
function form_submit(){
if(isset($_POST[\'form_sub\']))
{
//here you\'ll have your form data in a $_POST array, you can check it using a print_r. parse the form and insert the post
$title = $_POST[\'name\'];
$content = $_POST[\'comment\'];
//change the category and author as you want
$post_obj = array(
\'post_title\' => $title,
\'post_content\' => $content,
\'post_category\' => array(1), //Uncategorized
\'post_status\' => \'draft\',
\'post_author\' => 1 //Admin
);
$id = wp_insert_post($post_obj);
//check if successfully submitted
if(isset($id) && !is_wp_error($id))
{
//redirect to a thank you page, make sure the slug is \'thank-you\'
wp_redirect(home_url(\'/thank-you\'));
exit;
}
}
}
?>
试试这个。如果你卡住了,请告诉我。
编辑:表单模板应该是这样的。submit按钮应该具有name属性,而不是表单。
<?php /* Template Name: Submit */ ?>
<?php get_header(); ?>
Form this:
<form action="" method="post">
Name: <input type="text" name="name" />
Comment: <textarea name="comment"></textarea>
<input type="submit" value="Submit" name="form_sub" />
</form>
<?php get_footer(); ?>