0

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/");
exit;
}

form code:

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

<label for="title">Full name</label>
<br/>
<input class="form-control" id="formtitle" name="title" type="text"/>
<br/>
<label for="description">Your experience</label>
<br/>
<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" />
<br/>
<select name="categories" class="form-control" id="categories">
<option value="">Select</option>
<option value="intern">Intern</option>
<option value="host-company">Host Company</option>
</select>
<br/>
<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' ); ?-->
</div>

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

0

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

#categories
  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, privacy policy and cookie policy

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