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'm trying to use a front end form to create a post. I've been trying for a while now but I the post is not created. I'm using the code below, i'm not sure if i've forgot something.

<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/html-header','parts/shared/header') ); ?>

<?php 
function ty_front_end_form() {
?>
    <?php

    if($_POST){
        ty_save_post_data();
    }

}

function ty_save_post_data() {

    if ( empty($_POST) || !wp_verify_nonce($_POST['name_of_nonce_field'],'name_of_my_action') )
    {
       print 'Sorry, your nonce did not verify.';
       exit;

    }else{ 

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

        if(isset($_POST['post_tags'])){
        $tags = $_POST['post_tags'];
        }else{
        $tags = "";
        }

        // Add the content of the form to $post as an array
        $post = array(
            'post_title' => wp_strip_all_tags( $title ),
            'post_content' => $description,
            'post_category' => $_POST['cat'],  
            'tags_input' => $tags,
            'post_status' => 'publish',         
            'post_type' => $_POST['post-type'] 
        );
        wp_insert_post($post); 

        $location = home_url(); 

        echo "<meta http-equiv='refresh' content='0;url=$location' />"; exit;
    } // end IF

}
?>

<div class="form-wrap" style="margin-top: 300px;">

<form id="custom-post-type" name="custom-post-type" method="post" action="">

    <p><label for="title">Post Title</label><br />

    <input type="text" id="title" value="" tabindex="1" size="20" name="title" />


    <p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>

    <?php wp_nonce_field( 'name_of_my_action','name_of_nonce_field' ); ?>

</form>


</div>

<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/footer','parts/shared/html-footer') ); ?>
share|improve this question
    
It seems some fields don't have corresponding inputs, like the post type. Is there an input for the post type? –  engelen Jun 24 at 9:56
    
Post type is should always be set as Post –  Steven_Harris_ Jun 24 at 10:18
    
So why do you use $_POST['post-type'] instead of post (or omitting post_type altogether, as it's post by default)? –  engelen Jun 24 at 10:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.