I am trying to create a simple frontend form for posting

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.

Read More

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!

Related posts

Leave a Reply

1 comment

  1. 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.