this question has been answered on stackoverflow.
here is a link https://stackoverflow.com/questions/4321914/wp-insert-post-with-a-form/4321975#4321975
i’m trying to let users post to my site by using the wp_insert_post()
function..
<?php $postTitle = $_POST['post_title'];
$post = $_POST['post'];
$submit = $_POST['submit'];
if(isset($submit)){
global $user_ID;
$new_post = array(
'post_title' => $postTitle,
'post_content' => $post,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(7,100)
);
wp_insert_post($new_post);
}
?>
I got this hooked up to a form on a category page
<form method="post" action="">
<input type="text" name="post_title" size="45" id="input-title"/>
<textarea rows="5" name="post" 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 don’t know what id did wrong..it’s not working.
any ideas? thanks
wp_insert_post() makes use of the current user at several points, if memory serves.
So, you’d want to use wp_set_current_user() to switch that to some shared author user, and then switch it back to its original value when you’re done.
Alternatively, require users to be logged in and allow all groups to create drafts.
One problem you have is
you declare $submit on line above
thats why
always will return TRUE and execute your code.
I would although add
Then skip the post_date and post_type with your values those get added anyways. The less you pass to that function the less you can do wrong…
I would try to add this to your e.g. function.php script
This should insert a news post with the gioven values…
If this works the you know you have to look for the error in your form. I although think tht you cant pass categries to wp_insert_post….
But try the simple test first.