I've handrolled a form for posting from the front-end, with some custom functions that I needed. However, I've made the form only accessible when a user is logged in, and I'd like to have the articles show up as being posted by that user. Thoughts?? Here is my current code:
backend:
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && ($_POST['action']== 'new_post')) {
$has_errors = false;
$title = $_POST['title'];
$tablature = $_POST['tablature'];
$performer = $_POST['performer'];
$composer = $_POST['composer'];
$submitter = $_POST['submitter'];
$cat = array( $_POST['cat'] );
if(!isset($title)) { echo '<div class="error">Title required.</div>'; $has_errors = true; }
if(!isset($tablature)) { echo '<div class="error">Description required.</div>'; $has_errors = true; }
if(!isset($performer)) { echo '<div class="error">Performer required.</div>'; $has_errors = true; }
if(!isset($composer)) { echo '<div class="error">Composer required.</div>'; $has_errors = true; }
if($cat == -1) { echo '<div class="error">Please select a category.</div>'; $has_errors = true; }
$tags = $_POST['post_tags'];
if (!$has_errors){
// Save <title> by: <performer>
$title .= " by " .$performer;
// Save Composed by: <composer> Performed by: <performer> <tablature>
$content = "<h4>Composed by: ".$composer."</h4><h4>Performed by: ".$performer."</h4><p>Submitted by: ".$submitter."</p><br/>".$tablature;
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_category' => $cat,
'post_status' => 'draft'
);
wp_insert_post($new_post);
// save email and submitter as post meta in custom fields
wp_redirect( home_url('/thank-you/') );
}
}
frontend:
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="">
<p><label for="submitter">Submitted By</label><br />
<input type="text" id="submitter" value="" tabindex="1" size="20" name="submitter" class=”required” minlength="3" />
</p>
<p><label for="email">Email Address</label><br />
<input type="text" id="email" value="" tabindex="2" size="20" name="email" />
</p>
<p><label for="title">Song Title</label><br />
<input type="text" id="title" value="" tabindex="3" size="20" name="title" class=”required” minlength="3" />
</p>
<p><label for="composer">Composed By</label><br />
<input type="text" id="composer" value="" tabindex="4" size="20" name="composer" class=”required” minlength="3" />
</p>
<p><label for="performer">Performed By</label><br />
<input type="text" id="performer" value="" tabindex="5" size="20" name="performer" class=”required” minlength="3" />
</p>
<p><label for="tablature">Song Body</label><br />
<textarea id="tablature" tabindex="6" name="tablature" cols="50" rows="6" class=”required” minlength="50"></textarea>
</p>
<p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=7&taxonomy=category&exclude=5&class=required' ); ?></p>
<p align="right"><input type="submit" value="Submit" tabindex="8" id="submit" name="submit" /></p>
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
</div>
Thanks!