submit posts by unregistered users in wordpress

UPDATE – i made a simple plugin for this that adds the functionality:

  1. A widget that you can put in your sidebar that enables post to bee saved as pendling
  2. A widget that displays the three last submitted and confirmed posts.

The plugin have som simple css classes without any styling.

Here is the download link: Simple Frontend post

The function and form:

To get this to work you can use the function wp_insert_post to insert post/pages into the database, It sanitizes variables, does some checks, fills in missing variables like date/time, etc.

First you need a form to get all the necessary content that you need.

Here is a form that enables title and text content, put it where you want your form:

<form action="<?php echo site_url(); ?>/" method="post">
    <input type="text" id="title" value="" tabindex="5" name="title" />
    <textarea tabindex="3" name="desc" cols="5" rows="3"></textarea>
    <input type="submit" value="Submit" name="frontendpost">
</form>

And here is the function that put all the content into the database and save it as pendling, i.e you have to confirm it first before it will be seen on the front, put it into your funtions.php theme file:

<?php
    //Enable the front-end postings
    function save_frontend_post() {

        if ( !empty( $_POST ) && isset( $_POST['frontendpost'] ) ) {

            //Check to make sure that the post title field is not empty
            if( trim( $_POST['title'] ) === '' ) {
                $error = true;
            } else {
               $title = trim( $_POST['title'] );
            }

            //Check to make sure sure that content is submitted
            if( trim( $_POST['desc'] ) === '' )  {
                $error = true;
            } else {
               $desc = trim( $_POST['desc'] );
            }

            //If there is no error, send the form
            if( !isset( $error ) ) {

                //Create post object
                $new_post = array(
                    'post_title'     => $title,              //The title of your post.
                    'post_content'   => $desc,               //The full text of the post.
                    'post_date'      => date('Y-m-d H:i:s'), //The time post was made.
                    'post_status'    => 'pending',           //Set the status of the new post.
                    'post_type'      => 'post'
                );

                //Insert the post into the database
                $new_post = wp_insert_post( $new_post );        

            }
        }
    }
    add_action( 'wp_head', 'save_frontend_post', 10, 2 );
?>