Post from front-end with post types, categories and taxonomies

The question:

I am now developing a wordpress site that will be something like a directory. People will be able to submit walkthroughs, advanced reviews, as well as cheat codes for games. We were going to make a different form for each page, but now we’ve decided that one form with a drop down will be just fine.

There doesn’t seem to be a good plug in out there, that I can find, which accommodates the fact that each submission needs to be pushed through as a “pending post” but also to a certain post type.

For example, the form that we’ll use will be set up like this:

Game name: <post title>
Platform:  <Taxonomy>
Category:  <Category> (role playing, FPS, adventure, etc..)
This is a: () Review () Tutorial () Cheat list  <this is the post type>
Content: <post body>
Tags: <tags>
[Submit]

Upon submission, I need for the review, cheat, or tutorial to be set as a pending post, in the post type chosen from the radio boxes.

I’m currently using WP User Front End, but I could probably use any post from front-end form that is suggested for easiest modification. Adding the new fields to the form is easy, making them do stuff is hard! I’d appreciate any help I can get with this, our website sort of revolves around these feature.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

It looks like Gravity Forms can do this. I just did a quick search on the support forums and found this, an answer to someone asking almost exactly the same question as you:

Gravity Forms can be used to create
custom post types as well as custom
taxonomies, however it doesn’t do so
out of the box. It requires the use of
available hooks to tell Gravity Forms
to use a custom post type or custom
taxonomy instead of the default. By
default it uses standard WordPress
Posts, Categories and Tags.

So yes it is possible, but it does
take some custom code. When you are
ready to implement this you can search
the forums for code examples as many
people have done this, or you can post
a new post describing what you are
trying to do and request some
assistance and we can assist you with
basic code snippets to get you
started.

We do plan on creating an Add-On in
the future that will make it much
easier to create custom post types and
use custom taxonomies.

Method 2

there are some free plugins that let you submit posts from frontend:

and a better paid one would be:

But none of them will give you the flexibility as a custom coding to your needs,
and really a post from front end is a matter of displaying a form and processing it ,so following your use case your form would be something like this:

<!-- New game Post Form -->
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="">

<!-- game name -->
<p><label for="title">Game name</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title" />
</p>

<!-- game platform assuming that the taxonomy is named platform -->
<p><label for="Platform">Platform:</label><br />
<p><?php wp_dropdown_categories( 'show_option_none=Platform&tab_index=4&taxonomy=platform' ); ?></p>

<!-- game Category -->
<p><label for="Category">Category:</label><br />
<p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></p>

<!-- game post type assuming that the post types are named: review,tutorial,cheat_list-->
<p><label for="post_type">This is a:</label><br />
<p><select name="post_type" id="post_type">
    <option value="review">Review</option>
    <option value="tutorial">Tutorial</option>
    <option value="cheat_list"> Cheat list</option>
</select></p>

<!-- game Content -->
<p><label for="description">Content</label><br />
<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
</p>

<!-- game tags -->
<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_game_post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
</div>

and your form processing would be:

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,
        'post_category' => array($_POST['cat']),  // Usable for custom taxonomies too
        '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
    $pid = wp_insert_post($new_post); 
    //insert taxonomies
    wp_set_post_terms($pid,array($_POST['Platform']),'platform',true);
}

its not perfect but its a start and you should get the idea.

Hope this helps


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment