WordPress custom post type add form in frontend for users

wordpress-custom-posttype-formWordPress have nice features of custom post type. Sometimes we requires to make a form in frontend site where users can insert post . example in news/article site there should be a page where a user can submit news/article through form.
These all data goes to our custom post type with custom taxonomy and custom fields. Below i have decribed from begening about
– creating Custom Post Type i.e ‘new’ (plural name ‘news’)
– create custom taxonomy i.e ‘news-category’ and ‘state’
– create custom field ‘news-image’

I have created a post type ‘new’ (slug name ‘new’ ,singular name ‘news’) with the help of types plugins as seen below screenshot


Now let’s create a page template for ‘News Add’

I have created a php file ‘news_add.php’ and commneted inside the page

After accessing admin panel, create a page ‘News Add’ and select ‘News add’ on ‘Template’ combo box appeared on right side of page. Now we can access the page from ‘news-add’ after your site home url .in my contest
http://localhost/batch/b107wp/news-add/

Now i have called header footer and some div for the template layout . After created form inside this and processed as shown below code : I have mentioned some function ref. links for details overviews.


<?php
/*
Template Name:News Add

*/

?>


<?php get_header(); ?>

 
 
 
 
<div class="container">
   		 
		 <div class="left">
		 		 
		 <?php dynamic_sidebar('sidebar-1'); ?>
		 </div>
		 
		 <div class="content">
		 
		 <?php
		 if(isset($_POST['submit'])){
		 	$title=$_POST['title'];
			$body=$_POST['body'];
			$news_category=$_POST['news_category'];
			$state=$_POST['state'];
			
			// Create post object
$post = array(
  'post_title'    => $title,
  'post_content'  => $body,
  'post_type'     =>'new',
  'post_status'   => 'publish',
  'post_author'   => 1
);

// Insert the post into the database ref.https://codex.wordpress.org/Function_Reference/wp_insert_post
$post_id = wp_insert_post( $post, $wp_error='' );

if($post_id!=0){

	///upload image ref. https://codex.wordpress.org/Function_Reference/add_post_meta
	
	
	///ref. https://codex.wordpress.org/Function_Reference/wp_handle_upload
	if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['image'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
   // echo "File is valid, and was successfully uploaded.\n";
   // var_dump( $movefile);
} else {
    echo "Possible file upload attack!\n";
}

	
 	add_post_meta($post_id, 'wpcf-news-image', $movefile['url'] );
	
	///////////////////////////////////////////////////////////





	//insert taxonomy terms
	//ref. https://codex.wordpress.org/Function_Reference/wp_set_object_terms
	
	////for taxonomy 'news-category'//////////////////////////////
	$cat_ids = array($news_category);
	
	$cat_ids = array_map( 'intval', $cat_ids );
	$cat_ids = array_unique( $cat_ids );

	$term_taxonomy_ids = wp_set_object_terms($post_id, $cat_ids, 'news-category' );
	
	/////////////End for taxonomy 'news-category'//////////////////////////////
	
	
	
	////for taxonomy 'st'//////////////////////////////
	$cat_ids2 = array($state);
	
	$cat_ids2 = array_map( 'intval', $cat_ids2 );
	$cat_ids2 = array_unique( $cat_ids2 );

	$term_taxonomy_ids2 = wp_set_object_terms($post_id, $cat_ids2, 'st' );
	
	/////////////End for taxonomy 'news-category'//////////////////////////////
	
	

	echo "New successfully added";
}	

		 
		 }
		 
		 
		 ?>
		 
		 
		 
 		 <h1>Add News</h1>
		 
	<form action="" method="post" enctype="multipart/form-data">
	  
	  <table width="295" height="346" border="0">
  <tr>
    <td>Name</td>
    <td><input type="text" name="title" /></td>
  </tr>
  <tr>
    <td>Description</td>
    <td><textarea name="body" ></textarea></td>
  </tr>
  <tr>
    <td>Upload Image </td>
    <td><input type="file" name="image" /></td>
  </tr>
   <tr>
    <td>State</td>
    <td><?php
	//ref. https://codex.wordpress.org/Function_Reference/get_terms
	 $terms2 = get_terms("st",'order_by=count&hide_empty=0');
 if ( !empty( $terms2 ) && !is_wp_error( $terms2 ) ){
     echo "<select name='state'>";
	 echo "<option selected='selected'> Select </option>";
     foreach ( $terms2 as $term2 ) {
       echo "<option value='".$term2->term_id."'>" . $term2->name . "</option>";
        
     }
     echo "</select>";
 }
	?></td>
  </tr>
  <tr>
    <td>Category </td>
    <td>
	
	<?php
	//ref. https://codex.wordpress.org/Function_Reference/get_terms
	 $terms = get_terms("news-category",'order_by=count&hide_empty=0');
 if ( !empty( $terms ) && !is_wp_error( $terms ) ){
     echo "<select name='news_category'>";
	 	 echo "<option selected='selected'> Select </option>";

     foreach ( $terms as $term ) {
       echo "<option value='".$term->term_id."'>" . $term->name . "</option>";
        
     }
     echo "</select>";
 }
	?>
	
	</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit"  value="Submit"/></td>
  </tr>
</table>

	
	</form>
		 
		 </div>
		 
		 <div class="right">
		 <?php dynamic_sidebar('sidebar-4'); ?>
		 
		 </div>
		 
		 
		 
		 
		
		
		<div class="clear"></div>
		
		 
		 
   
   </div>
 
 
 
 
 
 
 
 
 
 
 
  
<?php get_footer(); ?>



Download this wordpress theme ‘news’

I think this tutorial will guide you understanding wordpress custom post type add form .
If you looking advance training visit our site WordPress training
or
call me +91 99999 680 96

  • Frédéric Beaupré

    Super your article. How to add a second field to also add a logo

Online Web Development Training,Video Tutorials