If your website allows guests or users to submit content on your site, such as blog posts, news articles, advertisements, etc., you need to provide them with the means to do so. There are two ways you can allow visitors to submit content on your site. One, is by providing them access to the dashboard, and the other is by providing an option in the front-end itself.
[space]
To allow content to be submitted from the front-end, you need to provide the following:
-
Create a Custom Post type to store the data. (For content other than blog posts)
-
Display a form to accept user data.
-
Map the data to post fields, and add the content in the database.
[space]
Provide a Form to accept a Custom Post Type Content
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. You can either create your own form, or use a plugin to create a form for you.
For example, you could create the form as follows:
[pre]<form method=”post”>
<p><label for=”cptTitle”><?php _e(‘Enter the Post Title:’, ‘mytextdomain’) ?></label>
<input type=”text” name=”cptTitle” id=”cptTitle” /></p>
<p> <label for=”cptContent”><?php _e(‘Enter Some Content:’, ‘mytextdomain’) ?></label>
<textarea name=”cptContent” id=”cptContent” 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>[/pre]
The form above has fields for post title and content. The custom post type name is also provided in the field ‘post_type’. You need to add basic validations to your form fields, using jQuery, before accepting the content. The nonce field is added for security purposes, to ensure that the request has come from the current site and not from any other.
Development tips:
-
If you want to display the form in multiple places, you could create a shortcode to display the form.
-
You can make additional customizations to decide if you want to allow guests or only logged in users, to submit content.
[space]
Inserting a Custom Post Type
The wp_insert_post function allows the creation and addition of a post into the database. You can use the same function, and the form field values, to insert a custom post type.
[pre]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[‘cptTitle’],
‘post_content’ => $_POST[‘cptContent’],
‘post_status’ => ‘pending’,
‘post_type’ => $_POST[‘post_type’]
);
// insert the post into the database
$cpt_id = wp_insert_post( $my_cptpost_args, $wp_error);
}[/pre]
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’.
A new custom post type will be created, with an id, whose value will be stored in $cpt_id. You can add additional meta information using this post id.
[space]
Using Gravity Forms to Submit Custom Post Types
By default, the Gravity Forms plugin allows the submission of a post type ‘post’. There are post fields available to readily accept post data, and a new post can be created, without a single line of code. Additionally, there is an extension plugin available, ‘Gravity Forms + Custom Post Types’, which allows the submission of a custom post type, using Gravity Forms.
An advanced forms plugin like Gravity Forms, provides several fields which you can use, to collect Custom Post type data. The Gravity Forms + Custom Post Types extension, allows mapping of form fields to custom post type content, and custom taxonomies.
[space]
Conclusion
Either way, you can easily create a user interface on your WordPress website front-end, to allow users to submit content for custom post type. This can provide for a more user friendly approach, especially when your site regularly accepts user submitted content.
8 Responses
I couldnt really figure this out 🙁
Hello Namrata,
Thanks for such a fantastic tutorial. I want to know if i want to add some more information like company and city.
I know for this i need to make custom metaboxes but if you have a quicky friendly solution. Kindly tell me
@shahank you can use Advanced Custom Fields for that. That is really quick solution.
Where do we put the php code containing wp_insert_post() ? The article makes no sense without mentioning that
@Ram. Please put the both codes in one file. Form example. Create a template file put all the code in that file and go on or create the a plugin file.
i have to agree with Ram Iyer on this one, its a detailed article but for those not so good in coding you need to explain where to post the code you have provided. is it in the wordpress admin panel or in the wp-config file or in a any other specific file ?
Yes man you are rite, without “insert” function, its useless !!
how to submit a custom taxonomy on front-end post submission?
it will be checked at backend