Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Custom Post Type: Insert post via Frontend
<?php
/**
* post-process.php
* make sure to include post-process.php in your functions.php. Use this in functions.php:
*
* get_template_part('post-process');
*
*/
function do_insert() {
if( 'POST' == $_SERVER['REQUEST_METHOD']
&& !empty( $_POST['action'] )
&& $_POST['post_type'] == 'book' ) { // Check what the post type is here instead
// Setting the 'post_type' => $_POST['post_type'] in the $post array below causes 404
// Just set it based on what is set in the above IF $_POST type == 'book'.
// and below do 'post_type' => 'book'
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) { $title = $_POST['title']; } else { echo 'Please enter a title'; }
if (isset ($_POST['description'])) { $description = $_POST['description']; } else { echo 'Please enter the content'; }
$tags = trim( $_POST['post_tags'] );
// Get the array of selected categories as multiple cats can be selected
$cat = array( $_POST['cat'] );
// Add the content of the form to $post as an array
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => $cat, // Usable for custom taxonomies too
'tags_input' => $tags,
'post_status' => 'publish', // Choose: publish, preview, future, etc.
'post_type' => 'book' // Set the post type based on the IF is post_type X
);
wp_insert_post($post); // Pass the value of $post to WordPress the insert function
// http://codex.wordpress.org/Function_Reference/wp_insert_post
} // end IF
}
// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'do_insert');
?>
<!-- Put this in your theme template file -->
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="<?php bloginfo('template_directory');?>/post-process.php">
<p><label for="title">Title</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title" /></p>
<p><label for="description">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">Tags</label>
<input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
<p><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>
<input type="hidden" name="post_type" id="post_type" value="book" />
<input type="hidden" name="action" value="post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
</div>
@eaglejohn
Copy link

eaglejohn commented Feb 26, 2013

@bueltge

Just copy/pasted your code in my theme and getting this error:

Fatal error: Call to undefined function do_action() in /home/domain.com/public_html/wp/wp-content/themes/theme/post-process.php on line 41

Only changed "book" to "post" since I'm not using a custom post type.

Any suggestions?

@Ronnieinspain
Copy link

Ronnieinspain commented Mar 4, 2013

I've made loads of changes to this script which i find great to use as a basis but was unfortunatly offline when i did so; upon uploading the code to test i got the same as @eaglejohn... Please could you check ? im not that well versed in WP as of yet... Danke !

Copy link

ghost commented Apr 11, 2013

thanks, helped me out a lot, for the other comments (@eaglejohn and @Ronnieinspain) i suspect you are not uploading the first part in to you functions.php (if the do_action()) is not available there you have bigger problems :)

@EricHamby
Copy link

EricHamby commented Apr 13, 2013

I cant get the cat drop down to pick up my custom taxenomies.

@oneblackcrayon
Copy link

oneblackcrayon commented May 20, 2013

@bueltge Is there a way to add an upload image to this?

@makbeta
Copy link

makbeta commented Jun 6, 2013

This code snippet didn't seem to work for me either. However the tutorial at http://wp.tutsplus.com/tutorials/creative-coding/posting-via-the-front-end-inserting/ seems to have done the charm for me.

Upon further research I found why I was getting a 404. When my form tag field was named 'tag' I would get a 404, as Wordpress has default handling for /tag. Example ... once I renamed it to .. in HTML that solved the issue. Hope this helps.

@iolufemi
Copy link

iolufemi commented Jul 23, 2013

It worked fine for me when i removed the wp_ounce function. tho, i had to use it as a page template.

@lukeseall
Copy link

lukeseall commented May 1, 2014

I can't get this working either. Could you just clarify what goes in functions.php, and what goes in post-process.php?

Thanks

@derniertec
Copy link

derniertec commented Dec 13, 2017

@derniertec
Copy link

derniertec commented Dec 13, 2017

@derniertec
Copy link

derniertec commented Dec 13, 2017

@vp956009
Copy link

vp956009 commented Nov 26, 2020

https://wordpress.org/plugins/orange-form/ this good plugin for do that but this is plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment