Post form as custom post

I’m new to WordPress and need to create a website including a special form, with which I can upload photos, set my position on an OpenStreetMap and choose from several dropdown menus.

Here you can find an example of how it should look like:
enter image description here

Read More

I already did this:

  1. Created a page template with the form

    <form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">

  2. Made the form submitable for users who are logged in (is this a secure solution?)

        <?php if ( 0 == $current_user->ID ) {
                         wp_loginout(); 
                         echo " to submit a post";
                    } else {
                      echo '<fieldset class="submit">
                                <input type="submit" value="Los" tabindex="40" id="submit" name="submit" />
                            </fieldset>
                            <input type="hidden" name="action" value="new_post" />';
                            wp_nonce_field( "new-post" ); } ?>
    
  3. When the form is submitted this php code is executed:

    if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {    
        $new_post = array(
                'post_title'    =>  "roadkill entry",
                'post_content'  =>  "testcontent",
                'post_category' =>  array(0),
                'post_status'   =>  'publish',
                'post_type' =>  'roadkills' // this is my custom post   );
    
        $pid = wp_insert_post($new_post);
    
        $link = get_permalink( $pid );
        wp_redirect( $link );
    }
    

But I will end in an page not found. http://www.citizen-science.at/wordpress/roadkill/eintrag-hinzufugen/

When I create an entry via the admin GUI I am able to view the single post, but not the archive http://www.citizen-science.at/wordpress/roadkills/dafdf/

Oh yeah, here is how I am registering my post type:

<?php 
add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'roadkills',
        array(
            'labels' => array(
                'name' => __( 'Roadkills' ),
                'singular_name' => __( 'Roadkill' )
            ),
        'public' => true,
        'has_archive' => true,
        )
    );
}

What did I miss here? (I already got it working yesterday, but somehow it broke)

Related posts

3 comments

  1. Within your theme folder create a page names archive-{YOUR_POST_TYPE}.php. In your case archive-roadkills.php. Test whether your archive page is catching this page or not?

    If yes, then you can edit the page the way you like. Whether copying the code from existing archive.php or your own.

    Reference: Template Hierarchy Codex

  2. I suggest to give the following code a try:

    <?php 
    add_action( 'init', 'create_post_type' );
    function create_post_type() {
        register_post_type( 'roadkills',
            array(
                'labels' => array(
                    'name' => __( 'Roadkills' ),
                    'singular_name' => __( 'Roadkill' )
                ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'roadkills')
            )
        );
    }
    

    I suppose the missing ‘rewrite’ => array(‘slug’ => ‘roadkills’) within your register post type function leads into the permalink error and can be solved by adding the line above.

  3. I could only solve the problem by creating another custom post type (with another name). Afterwards everything just went fine. I am sorry, but I don’t have any explanation for this.

Comments are closed.