1

This is a repost of a question asked on stackoverflow, and was referred to this site:

My client wants to create a form on his new WP site that when filled out and submitted will be submitted to his admin post queue to approve and if he approves gets posted on his site in the "blog" (which is actually a bunch of guitar like tabs). The form would be custom and have custom fields. Below is the form, but in the old design before I did a refresh on it.

So, how hard would this be? He does not want it in the WP admin panel which i began to do, but outside in a page like /contribute

2 Answers 2

1

you can use plugins fro the front-end posting:

Or you can create the form yourself

    <!-- New Post Form -->
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="">
<p><label for="title">Title</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title" />
</p>
<p><label for="description">Description</label><br />
<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
</p>
<p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></p>
<p><label for="post_tags">Tags</label>
<input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
</div>
<!--// New Post Form -->

and process it

   if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  ($_POST['action']== 'new_post')) {

    if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
    } else {
        echo 'Please enter a title';
    }
    if (isset ($_POST['description'])) {
        $description = $_POST['description'];
    } else {
        echo 'Please enter the content';
    }
    $tags = $_POST['post_tags'];

    $new_post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'post_category' => $_POST['cat'],  
        'tags_input'    => $tags,
        'post_status'   => 'draft'
    );
    wp_insert_post($new_post);  

}

And make sure to check for nonce and sanitization of the form fields.

UPDATE

According to your gist code and comment change your function to this:

 if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && ($_POST['action']== 'new_post')) {
        $has_errors = false;
        if (isset ($_POST['title'])) {
            $title = $_POST['title'];
        } else {
            echo 'Please enter a title';
            $has_errors = true;
        }
        if (isset ($_POST['performer'])) {
            $performer = $_POST['preformer'];
        } else {
            echo 'Please enter a performer';
            $has_errors = true;
        }
        if (isset ($_POST['composer'])) {
          $composer = $_POST['composer'];
        } else {
            echo 'Please enter a composer';
            $has_errors = true;
        }
        if (isset ($_POST['tablature'])) {
            $tablature = $_POST['tablature'];
        } else {
            echo 'Please enter the content';
            $has_errors = true;
        }
        $tags = $_POST['post_tags'];
        if (!$has_errors){
        //save <title>  by: <preformer>
        $title .= " by: " .$performer;

        //save Composed by: <composer> Performed by: <performer> <tablature>
        $content = "<h4>Composed by: ". $composer."</h4><br/><h4>Performed by: ".$performer."</h4><br/>".$tablature;

        $new_post = array(
            'post_title' => $title,
            'post_content' => $content,
            'post_category' => $_POST['cat'],
            'tags_input' => $tags,
            'post_status' => 'draft'
        );
        $pid = wp_insert_post($new_post);


        //save email and submmiter as post meta in custom fields
        update_post_meta($pid, 'submiter_email', urldecode($_POST['email']));
        update_post_meta($pid, 'submiter_name', urldecode($_POST['submitter']));
    }
}
5
  • I'm no PHP programmer, nor do I pretend to be, but here's what I came up with: gist.github.com/810767. The idea is that the output be "<song title> by <performer>" in the title, and "<h4>Composed by: <composer></h4><br/><h4>Performed by: <performer></h4><br/><description>" in the content area. This code created a new post draft, but that draft was empty, other than the title. I think I'm close, but not quite there. Thanks a million for this answer, it's definitely got me on the right track!! Commented Feb 4, 2011 at 5:18
  • you are not saving any of the fields to your new post.
    – Bainternet
    Commented Feb 4, 2011 at 11:24
  • I updated the answer
    – Bainternet
    Commented Feb 4, 2011 at 11:38
  • Thanks again for your help! This is where I'm at at this point, and it's nearly finished, but I'm stuck on three things. The categories don't post, the title posts as <title> by: <title> instead of <title> by: <performer>, and the title seems to show up initially in the listing of posts, but when I click on the title to go to the edit post page, the title bar is empty? gist.github.com/35ad2c03a340bb324109 Commented Feb 5, 2011 at 6:09
  • So in addition to the title field showing up as empty when entering the 'edit post' page, the permalink DOES have the song title in permalink form. Weird. Looks like also the form is outputting "Performed by: <song title>". I really appreciate your help! Commented Feb 5, 2011 at 6:57
0

I haven't checked it out, but this plugin probably shows you how to approach it. Here is another one, or you could create a special user called 'anonymous' and give them 'author' permissions. You probably want to be careful about giving them edit or delete permissions.

2
  • I had previously been trying to make this work with TDOMF, but the 'hack form' was way too complicated for me. If I can't get this written on my own (by which I mean, with the help of folks like you) I'll give anon-posting a shot! Thanks! Commented Feb 4, 2011 at 6:15
  • I updated the code
    – Bainternet
    Commented Feb 5, 2011 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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