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/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s