带表单的wp_insert_post [英] wp_insert_post with a form
本文介绍了带表单的wp_insert_post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?php
$submitted = $_POST['submit'];
$post-title= $_POST['post_title'];
$post-content= $_POST['post_content'];
$new_post = array(
'post_title' => '$post-title',
'post_content' => '$post-content',
'post_status' => 'publish',
'post_author' => $user_ID,
);
if(isset($submitted)){
wp_insert_post($new_post);
}
?>
<form method="post" action=" ">
<input type="text" name="post_title" size="45" id="input-title"/>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea>
<input type="hidden" name="cat" value="7,100"/>
<input class="subput round" type="submit" name="submit" value="Post"/>
</form>
我在没有表格的情况下对其进行了测试,并且效果很好.但出于某种原因,我似乎无法使其与表格一起使用.有什么想法吗?
I tested it with out the form and it worked fine. but for somereason i cant seem to get it to work with the form. any ideas?
该表格的操作中还应该包含什么内容?
also what is supposed to go in the action for the form?
感谢您的帮助.
推荐答案
这应该有效.
<?php
if(isset($_POST['new_post']) == '1') {
$post_title = $_POST['post_title'];
$post_category = $_POST['cat'];
$post_content = $_POST['post_content'];
$new_post = array(
'ID' => '',
'post_author' => $user->ID,
'post_category' => array($post_category),
'post_content' => $post_content,
'post_title' => $post_title,
'post_status' => 'publish'
);
$post_id = wp_insert_post($new_post);
// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect($post->guid);
}
?>
<form method="post" action="">
<input type="text" name="post_title" size="45" id="input-title"/>
<?php wp_dropdown_categories('orderby=name&hide_empty=0&exclude=1&hierarchical=1'); ?>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea>
<input type="hidden" name="new_post" value="1"/>
<input class="subput round" type="submit" name="submit" value="Post"/>
</form>
如果输入的名称为 new_post ,值为0,则添加该帖子.表单无需采取任何措施,但应将PHP部分保留在标题的顶部.
If input with name new_post and value of 0 then add the post. No action needed for the form, but you should keep the PHP part on the top of the header.
这篇关于带表单的wp_insert_post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文