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.

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!

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

you can use

global $current_user;
get_currentuserinfo();

and then add

'post_author' => $current_user->ID

to your $new_post array.

then you can use

<p>Posted by: <?php the_author(); ?></p>
share|improve this answer
    
This worked fantastically. Thanks a million! –  Unfortunately Feb 11 '11 at 22:52
    
Glad i can help –  Bainternet Feb 11 '11 at 22:54
    
Actually, I can see the use who posted the article in the admin, but in the post, it's blank. –  Unfortunately Feb 11 '11 at 23:20
    
here's my backend code: gist.github.com/d5b35d46114b20d73639 –  Unfortunately Feb 11 '11 at 23:48
    
the backend is the admin and you said you can see it there. if you mean the front end then add <p>Posted by: <?php the_author(); ?></p> to your loop –  Bainternet Feb 12 '11 at 0:09
show 6 more comments

Assuming the user is logged in, then wp_insert_post should automatically set the post author field properly. Have you tried just calling the_author() to output the author of the posts name?

share|improve this answer
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.