wp_insert_post let users post to without login…broke

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..

Read More
<?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

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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.