Creating Client Testimonials With Custom Post Types

If you're running any type of business online, it's a good idea to get your clients' opinions on what they thought about the services you provided.

First of all, this can benefit you by giving you feedback on how you can improve aspects of your business, but most of all, it can give you great testimonials, which can help to persuade potential clients to use your services.

The easiest way to add this functionality to your site would be to include it as a plugin. I've put together all the necessary files and included a download link for the Client Testimonials plugin above.


Testimonial Custom Post Type

Custom Post Types are great for separating your content according to different needs. Especially if your custom content doesn't need all the bells and whistles of a straight-up post.

For this tutorial, I'm going to show you how you can quickly create a Custom Post Type for your testimonials that only requires the text editor and three custom meta boxes.


Adding a Metabox

Now that a Custom Post Type for your testimonials has been created and you've established a callback for the custom metaboxes, you need to set up how those metaboxes will be displayed. So next up you need to use the add_meta_box() function to do just that.

There are three fields you should include when setting up the data for your testimonial: the client's name, their business and a link to their site. Sometimes, you might not have all three but the least amount of information you should require is the client's name.

Tip: Whenever you add a metabox, be sure to use a nonce to secure the form. It's a must. Read more about nonces in the WordPress codex.

Saving the Custom Meta

Since you've added a custom metabox, you'll need to make sure that all the data is validated and saved. You need to hook into the save_post action and set up a callback function.


Customizing the List View

After you've created your first testimonial, you'll see it appear in the list view of your Custom Post Type; however, you won't see any of the custom meta data.

That's an easy fix: You just need to add a couple more functions to customize the list view columns so that all the info you want to see will appear.

That's pretty much everything you need to set up testimonials in the WordPress admin. But what about displaying them on the front end? Let's look at a few different ways to display your testimonials.


Display Testimonials

If you'd like to display a testimonial somewhere in one of your theme's page templates, you'll need to create a function to do so. Here's a quick one that'll allow you to display client testimonials. You can use the parameters to select one specific testimonial using an ID, or even display a random one by passing an 'orderby' value.

Here's the CSS that I use to style the testimonials.


Testimonials Shortcode

You might also want to display testimonials within your post or page content. That's not a problem. All you need to do is hook into the WordPress Shortcode API.


Testimonials Widget

Widgets are great. They're easy to use and can add so much functionality to your site. So let's set up a simple testimonials widget so you can display your client's testimonials in any of your theme's widgetized areas.


Testimonials Archive Page Template

Since testimonials require custom meta, you can't rely on the default archive page template to display them correctly. In order to set up a custom archive page, you need to create a file called archive-testimonials.php and add it to your theme's main folder.


Conclusion

Hopefully you won't feel too overwhelmed by the amount of code above. You might not actually have to use it all since it really depends on what your needs will be. You might only need the shortcode or just the archive template. Either way, going through this tutorial should prepare you for many situations that you might encounter when adding client testimonials to your site.

If you have any comments or feedback on anything you read above, please feel free to discuss it below.

Tags:

Comments

Related Articles