Integrating an “Add Testimonial” form with TB-Testimonials

Dec 16 2012 0 Comments

I’ve had a couple people ask about having a form on their websites where people can submit their own testimonials to be approved so I figured why not write something teaching people how to do it.

Creating the form

First thing first we need to create a page template with the form for our page. We’re going to use a basic form with 5 fields; Name, Email, Company, Website URL, and Testimonial Body.

My code for this looks like so:

After you get the form page all done you can go ahead and create a new page in your wp-admin and apply the template to it. Preview it and see if you need to edit any CSS or anything. Most likely you will in order to get it to look presentable.

Now the form’s finished, let’s submit this thing and validate some user submitted data…

You’ll notice I left the action for the form blank. That’s so the form submits to the page we are currently on. You will also notice that the names of all my input fields are an array named new_testimonial[], that is so we can easily tell when the data is being submitted without a big huge conditional checking for $_POST['this'] or $_POST['that'].

First we need to validate the data that is being submitted. We do that like so:

Now if you noticed, we add our own action hook named tbtestimonial_messages that handles the output of the error and success messages for our form. we will need to add 2 functions to handle these. They look like this:

You can display those messages ANYWHERE in your form by adding do_action( 'tbtestimonial_messages'); anywhere in your form. I’m going to put it right after the entry-content div at the top of the form.

Now we’re cooking, all that’s left is to insert the testimonial into WordPress

Now that we have created the form and validated some user input to make sure that it is in fact what we want it to be, we can move on to inserting the testimonial (post) into WordPress and attaching the meta-data for the user’s company name, email address, and website url.

We do that like this:

Finished Page Template for Adding Testimonials

Hope that helps! Also you could use wp_mail() to send you a notification letting you know that a new draft testimonial has been created and you should go and either publish it or delete it. Food for thought!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.