WordPress: Post From The Front End With A Custom Post type

First We need to create the form here to be able to post in the front end. We will be using this in our template files or as a short code, depending on the style of coding.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    <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>
 
<p><label for="description">Post 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">Post 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="post-type" id="post-type" value="custom_posts" />
 
<input type="hidden" name="action" value="custom_posts" />
 
<?php wp_nonce_field( 'name_of_my_action','name_of_nonce_field' ); ?>
 
</form>

Save Post Data——————-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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'],  // Usable for custom taxonomies too
'tags_input' => $tags,
'post_status' => 'publish',            // Choose: publish, preview, future, etc.
'post_type' => $_POST['post-type'// Use a custom post type if you want to
);
 
$location = home_url(); // redirect location, should be login page
 
echo "<meta http-equiv='refresh' content='0;url=$location' />"; exit;
} // end IF
 
}

For More Please check
http://teachingyou.net/wordpress/wordpress-post-from-the-front-end-with-a-custom-post-type/

Code to register the custom post type

Use the following code to register custom the post type

Example

portfolio : Your Custome Post Type Name
function portfolio_register(){
    $labels = array('name'=> _x('Portfolio','post type general name'),'singular_name'=> _x('Portfolio Item','post type singular name'),'add_new'=> _x('Add New','portfolio item'),'add_new_item'=> __('Add New Portfolio Item'),'edit_item'=> __('Edit Portfolio Item'),'new_item'=> __('New Portfolio Item'),'view_item'=> __('View Portfolio Item'),'search_items'=> __('Search Portfolio Items'),'not_found'=>  __('Nothing found'),'not_found_in_trash'=> __('Nothing found in Trash'),'parent_item_colon'=>'');
    $args = array('labels'=> $labels,'public'=>true,'publicly_queryable'=>true,'show_ui'=>true,'query_var'=>true,'rewrite'=>true,'capability_type'=>'post','hierarchical'=>false,'menu_position'=>8,'supports'=> array('title','editor','thumbnail')); 
    register_post_type('portfolio', $args );}
add_action('init','portfolio_register');