1

I have created an 'event' custom post type and now I want to create a page that will allow users to create a custom post type from the front-end. Is there a standard way of doing this? I could create a template and then link it to a page that I create manually in the wordpress backend, but I would rather do it programatically if possible. Any suggestions?

2 Answers 2

1

So you want to allow people to create posts from the front-end? Sure...

In this answer here I explain a very basic way in which you can achieve this very thing, front-end posts to a post type of your choice.

Here is a basic example of what you might include in your template file to do so;

(please see original answer for further details)

<?php
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "my_post_type") {

//store our post vars into variables for later use
//now would be a good time to run some basic error checking/validation
//to ensure that data for these values have been set
$title     = $_POST['title'];
$content   = $_POST['content'];
$post_type = 'my_custom_post';
$custom_field_1 = $_POST['custom_1'];
$custom_field_2 = $_POST['custom_2'];    

//the array of arguements to be inserted with wp_insert_post
$new_post = array(
'post_title'    => $title,
'post_content'  => $content,
'post_status'   => 'publish',          
'post_type'     => $post_type 
);

//insert the the post into database by passing $new_post to wp_insert_post
//store our post ID in a variable $pid
$pid = wp_insert_post($new_post);

//we now use $pid (post id) to help add out post meta data
add_post_meta($pid, 'meta_key', $custom_field_1, true);
add_post_meta($pid, 'meta_key', $custom_field_2, true);

}
?>
<!-- language: lang-html -->
<form method="post" name="front_end" action="" >
<input type="text" name="title" value="My Post Title" />
<input type="text" name="content" value="My Post Content" />
<input type="text" name="custom_1" value="Custom Field 1 Content" />
<input type="text" name="custom_2" value="Custom Field 2 Content" />
<button type="button">Submit</button>
<input type="hidden" name="action" value="my_post_type" />
</form>

This example is void of any error checking, validation and sanitization. This just exemplifies how to take form data and store it into a post type.

4
  • using your method, I would create a page manually in the WordPress back-end and associate it with a template file?
    – William
    May 20, 2013 at 19:18
  • 1
    Yes, the above code can be placed in a custom template file or any existing theme template file. If it's the former, then you can create a page in the dashboard/backend and associate your newly created template with that page. Alternatively you could name the template file page-{slug}.php and that will automatically be used when viewing the page of 'slug' name, for example if you created a page called submit, then you could name your template page-submit.php, otherwise you can set the template from within the post edit screen. Also include get_header/footer calls in your template.
    – Adam
    May 21, 2013 at 5:42
  • stupid question: what is $_SERVER['REQUEST_METHOD'] for?
    – William
    May 21, 2013 at 14:51
  • 1
    Holds the, server request method, such as if it was a GET request or a POST request, etc. In this instance we are checking to make sure that the request method is a POST request (i.e. someone is actually submitting the form for real).
    – Adam
    May 21, 2013 at 14:57
0

I've done a handful of projects that utilize something like this, and here's my preferred solution:

  1. Create a new php page template within your theme.
  2. Use PHP to display a form that passes submitted values to itself, and posts the new data using the WordPress XML-RPC API. Normally, I'd create a new user that can only submit drafts to avoid auto-publishing spam or incorrect details.
  3. Create the page/URL where the post will be displayed and use the file you created in #1 as your custom template.
  4. Optional: Depending on the complexity of your form and the user interaction you intend to implement, you might also need to setup custom query variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.