If you want your website allows guests or users to submit wordpress post content on your website from front-end and you have full permission to publish and unpublished that content from back-end then use the following process:

Add custom post panel in backend:

/**custom post function**/
add_action( 'init', 'create_post_for_pro' );
   function create_post_for_pro() {
	register_post_type( 'meal',
    array(
      'labels' => array(
        'name' => __( "Meal" ),
        'singular_name' => __( "Meal" ),
        'all_items'=> __("All Meal"),
        'edit_item' => __("Edit Meal"),
        'add_new' => __("Add Meal"),
        'add_new_item' => __("Add New Meal"),
        'new_item'=> __("Add New Meal")
      ),
      'rewrite' => array( 'slug' => 'protein','with_front' => FALSE),
	  'capability_type' =>  'post',
      'public' => true,
	  'supports' => array(
	  'title',
	  'editor',
	  'excerpt',
	  'revisions',
	  'thumbnail',
	  'custom-fields',
	  )
    )
  );
}

Now use the following code where you want to show custom post on frontend:

<div class="meal">
    <h3>Meal</h3>
	<?php
	query_posts(array(
		   'post_type' => 'meal',
		   'showposts' => '50',  // -1 to all post
		    'order' => 'ASC',
		    'orderby' => 'start',
	                'show_past_events'=>false,
		   ));
	if (have_posts()) :
                     while (have_posts()) : the_post();
			 ?>
		  	<div class="block">
		           <strong><p><?php echo get_the_title();?></p></strong>
			<?php echo the_excerpt();?> 
	                  </div>
		     <?php 
			endwhile;
		    endif;		 
	?>
</div>

Now add front end form for users to submit content in a custom template:

<form method=”post”>

<p><label for=”wpTitle”><?php _e(‘Post Title:’, ‘mytextdomain’) ?></label>

<input type=”text” name=”wpTitle” id=”wpTitle” /></p>
<p><label for=”wpContent”><?php _e(‘Content:’, ‘mytextdomain’) ?></label>
<textarea name=”wpContent” id=”wpContent” rows=”4″ cols=”20″></textarea> </p>
<button type=”submit”><?php _e(‘Submit’, ‘mytextdomain’) ?></button>
<input type=”hidden” name=”post_type” id=”post_type” value=”meal” />
<?php wp_nonce_field( ‘wp_nonce_action’, ‘wp_nonce_field’ ); ?>
</form>

Now add the code to insert frontend content:

if (isset( $_POST[‘wp_nonce_field’] )

&& wp_verify_nonce( $_POST[‘wp_nonce_field’], ‘wp_nonce_action’ ) ) {

// create post object with the form values

$my_wppost_args = array(

‘post_title’    => $_POST[‘wpTitle’],

‘post_content’  => $_POST[‘wpContent’],

‘post_status’   => ‘pending’,

‘post_type’ => $_POST[‘post_type’]

);

// insert the post into the database

$wp_id = wp_insert_post( $my_wppost_args, $wp_error);

Now admin can publish, edit and delete post from backend.

Related Article
Categories: Blog

0 thoughts on “Submit WordPress Posts From The Frontend”

Leave a Reply