To start. I fairly new with php and finding my way around.

I have created a form (to capture customer testimonials on the front end) and create a draft custom post type called "testimonials" It works now (sadly without PHP validation only script validation)

What I get now is:

  • Post title
  • post content

What i want to do is to create a drop down that contains two categories

  1. Intern
  2. host-company

and POST those categories to my custom post type.

PHP code below is inside of my functions.php:

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
    $title =  $_POST['title'];
} else {
    echo 'Please enter a  title';
if (isset ($_POST['description'])) {
    $description = $_POST['description'];
} else {
    echo 'Please enter the content';
$tags = $_POST['post_tags'];

// Add the content of the form to $post as an array
$new_post = array(
    'post_title'    => $title,
    'post_content'  => $description,
    'tags_input'    => array($tags),
    'post_status'   => 'draft',           // do i need a comma after 'draft'?
    'post_type' => 'testimonial'  //do i need a comma after 'testimonial'?
//save the new post
$pid = wp_insert_post($new_post); 
header("Location: http://www.aipptraining.com.au/testimonial-submission-successful/");

form code:

<div class="form-group" id=="testimonialform">
<form action="" method="post" onsubmit="return validate();">

<label for="title">Full name</label>
<input class="form-control" id="formtitle" name="title" type="text"/>
<label for="description">Your experience</label>
<textarea class="form-control" id="description" cols="40" name="description" rows="4"></textarea>
<input class="form-control" tabindex="5" name="post_tags" size="16" type="hidden" value="Testimonial" />
<select name="categories" class="form-control" id="categories">
<option value="">Select</option>
<option value="intern">Intern</option>
<option value="host-company">Host Company</option>
<input class="btn btn-default" id="submit" tabindex="6" name="submit" type="submit" value="Submit" />
<input name="action" type="hidden" value="new_post" /></form>
<!--?php wp_nonce_field( 'new-post' ); ?-->

I would appreciate it if your answer was very specific as my understanding of PHP is limited.

Also aside from pulling category if you can help me validate the form that would be great. I was also trying to add an upload field for customers to upload their image but that was way too hard for me.

Thank you MJ

1 Answer 1


Its pretty much what you already have, just add the following after $pid = wp_insert_post($new_post);:

  if( !empty ( $_POST ['categories'] ) ){
    update_post_meta ( $pid, "_testimonials_category", stripslashes ( $_POST ['categories'] ) );    

I would probably advise you to change the name of this from categories though to something more relative, like testimonials_categories

Then you can use:

$chosen_testimonial_category = get_post_meta($pid,'_testimonials_category',true);

As for validation, check out jQuery.validate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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