Technology WordPress Tips & Tricks

Submit a Custom Post Type in WordPress from the Front End

Letting users submit custom post types from the front end powers directories, listings, and user generated content. Here is how to do it in WordPress, so your visitors can contribute content without back end access.

Namrata Namrata 4 min read
Submit a Custom Post Type in WordPress from the Front End

Submit PostIf 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:

[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.

Get a FREE Consultation

Let's build something that lasts.

Share what's on your mind — a clear brief, a half-formed idea, or just a sense that something needs to change. We'll listen first, ask the right questions, and point you toward what's actually worth building.

We take on a handful of projects each quarter,ones where we can truly make a difference.

  • Receive a human response within 24 hours
  • Get a detailed scope and quote upfront
  • We're happy to sign an NDA upon request

    Free 30-Min Strategy Call

    Your Name *

    Your Phone No *

    Work Email *

    Your Budget*

    Project Details *