To allow users to submit content, you need to provide a form. The form fields will be used to input details, such as title, post content, tags, categories, featured image, etc.
For example, you could create the form as follows:
<form method=”post”><p><label for=”stackTitle”><?php _e(‘Enter the Post Title:’, ‘mytextdomain’) ?></label>
<input type=”text” name=”stackTitle” id=”stackTitle” /></p>
<p> <label for=”stackContent”><?php _e(‘Enter Some Content:’, ‘mytextdomain’) ?></label>
<textarea name=”stackContent” id=”stackContent” rows=”4″ cols=”20″></textarea> </p>
<button type=”submit”><?php _e(‘Submit’, ‘mytextdomain’) ?></button>
<input type=”hidden” name=”post_type” id=”post_type” value=”my_custom_post_type” />
<?php wp_nonce_field( ‘cpt_nonce_action’, ‘cpt_nonce_field’ ); ?>
</form>
Inserting a Custom Post Type
Insert function create in post type in post type value.
if (isset( $_POST[‘cpt_nonce_field’] )&& wp_verify_nonce( $_POST[‘cpt_nonce_field’], ‘cpt_nonce_action’ ) ) {
// create post object with the form values
$my_cptpost_args = array(
‘post_title’ => $_POST[‘stackTitle’],
‘post_content’ => $_POST[‘stackContent’],
‘post_status’ => ‘pending’,
‘post_type’ => $_POST[‘post_type’]
);
// insert the post into the database
$cpt_id = wp_insert_post( $my_cptpost_args, $wp_error);
}
Set the post_status as ‘pending’ if you want the post to be approved by the admin, before being published. Else, set it to ‘published’.
Hi guyes
Nice tutorial and first time front end post submit so i will sucessfully insert post in admin site.
Thanks stackwordpress Team.
Thanks Ravi