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 am trying to learn and create some deeper things with Wordpress. My goal is to create a code/script/plugin that will allow me to post from the frontend.

I have seen many plugins, like templatic classifieds theme that submit, handle and edit posts from the frontend but are very complex with useless code for my purpose.

I would like to have a clearer view on how to get the ID, how to set a title, content and custom fields of course.

Once I go through this, I want to study it according to user_meta.

Thank you for your examples, codes, ideas, links!

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

To post from the front-end you can use wp_insert_post() function.

So its simply a matter of a form and processing it

the form:

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

the processing:

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

    // Do some minor form validation to make sure there is content
    if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
    } else {
        echo 'Please enter a game  title';
    }
    if (isset ($_POST['description'])) {
        $description = $_POST['description'];
    } else {
        echo 'Please enter the content';
    }
    $tags = $_POST['post_tags'];

    // Add the content of the form to $post as an array
    $new_post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'tags_input'    => array($tags),
        'post_status'   => 'publish',           // Choose: publish, preview, future, draft, etc.
        'post_type' => $_POST['post_type']  // Use a custom post type if you want to
    );
    //save the new post and return its ID
    $pid = wp_insert_post($new_post); 


}

Bonus Once you have the post id ($pid in the above example) then you can easily set terms and custom fields:

 //insert taxonomies like categories tags or custom 
    wp_set_post_terms($pid,(array)($_POST['cat']),'category',true);

//insert custom fields
update_post_meta($pid,'meta_key_name',$_POST['meta_value']);

hope this helps.

share|improve this answer
    
I really helps, thank you! What are the terms for? So in the processing form I put the "insert custom fields" code with the correct id/name on the form right? What about if I want to edit a post from frontend ? –  Punkis Apr 21 '11 at 2:26
    
Hi, how can I delete or edit a post from the frontend? –  Punkis May 16 '11 at 14:05
    
Ask this as a separate question –  Bainternet May 16 '11 at 14:19
    
    
@Punkis i answered that question. –  Bainternet May 31 '11 at 7:40
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.