Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

this question has been answered on stackoverflow. here is a link http://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

share|improve this question
    
actually thats me here it's correct on the actual file –  andrewk Oct 19 '10 at 6:58
    
What is the return value of wp_insert_post()? Could you add var_dump() around it? It returns the new ID if succesful, and a WP_Error on failure. The latter should contain an error message. –  Jan Fabry Oct 19 '10 at 7:03
    
not successful and no error..just the same –  andrewk Oct 19 '10 at 7:26
    
A general cheap debugging tip: place var_dump(__LINE__) in the wp_insert_post() function, at various places, to find out until where the code executes. It should print out line numbers, and if it stops, you know the following var_dump() is not reached. Repeat as necessary to pinpoint the problem. We need more specific information to be able to help you. –  Jan Fabry Oct 19 '10 at 9:42
add comment

2 Answers

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.

share|improve this answer
    
it does not work even when i (admin) am logged in...confused –  andrewk Oct 20 '10 at 4:59
add comment

One problem you have is

if(isset($submit)){

you declare $submit on line above

$submit = $_POST['submit'];

thats why

isset($submit)

always will return TRUE and execute your code.

I would although add

global $user_ID;
if ( $user_ID )
{
     //insert your post
}
else
{
     //give that person a message "Dude, you have to sign up or login to be able to submit content..."
}

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

$new_post = array(
            'post_title' => 'Test Post Title',
            'post_content' => 'Test Post Content',
            'post_status' => 'publish',
            'post_author' => $user_ID,

        );
wp_insert_post($new_post);

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.

share|improve this answer
    
when i tested it in function.php it worked fine...but i'm still not sure why it't not working...i will look around –  andrewk Oct 20 '10 at 4:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.