Blog » Client Testimonials & WordPress Custom Post Type

February 11th, 2012

Lucky to work with great clients from a variety of industries, we are luckier still that our clients were willing to share some positive feedback on our work. Given that our relationship with some of these clients spans more than a decade, we felt their testimonials would carry weight with potential new clients, so we of course posted these testimonials to our site.

Our website runs on WordPress, the open-source CMS/blog infrastructure. Since more testimonials would come in over time, and since testimonials really are not WordPress “pages” nor “posts,” this seemed a good scenario in which to use custom post types. WordPress 3 (3.3.1 is the most-recent version, as of this writing) allows for pretty easy creation of a custom type of post – that is, a collection of items all belonging to some common category. You might, for instance, create a custom type for “Products,” for “Springsteen Songs,” for “Inventory Items” – any set of nouns sharing some common features and which you wish to handle differently from the built-in WordPress posts and pages. In our case, we created type “Testimonial” – each item would be a client testimonial.

Creating a new custom post type is fairly straightforward. (We assume here, dear reader, that you both understand how to and are interested in editing some of the PHP files that make up WordPress.) In your theme’s functions.php file, add a function named similar to create_testimonial_post_type() and hook it to WordPress’s init via add_action, as such:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
add_action( 'init', 'create_testimonial_post_type' );
function create_testimonial_post_type() {
    register_post_type( 'testimonial',
        array(
            'labels' => array(
                'name' => __( 'Testimonials' ),
                'singular_name' => __( 'Testimonial' ),
                'add_new' => __( 'Add New' ),
                'add_new_item' => __( 'Add New Testimonial' ),
                'edit' => __( 'Edit' ),
                'edit_item' => __( 'Edit Testimonial' ),
                'new_item' => __( 'New Testimonial' ),
                'view' => __( 'View Testimonial' ),
                'view_item' => __( 'View Testimonial' ),
                'search_items' => __( 'Search Testimonial' ),
                'not_found' => __( 'No testimonials found' ),
                'not_found_in_trash' => __( 'No testimonials found in Trash' ),
                'parent' => __( 'Parent Testimonial' ),
            ),
            'public' => true,
            'has_archive' => true
        )
    );
}

The labels hash defines the name of the type, singular and plural, in various contexts; the register_post_type function offers, unsurprisingly, a variety of parameters by which you can control the presentation and behavior of the custom type, both in WordPress’s admin CMS and in the public-facing pages. The WordPress docs suggest, appropriately, that one should namespace function names to avoid conflicting with future theme or plugin. We chose to ignore this – I’m sure it will come back to bite us.

Wordpress CMS admin dashboard with custom post typeAdding this minimal amount of code immediately makes the new custom post type show in the WordPress admin sidebar. Note, in the screenshot at left, the “Testimonials” category – not a post, page, or other type of WordPress element, but a created-by-us custom type of item.

Conveniently, and following the useful WordPress convention, template files single-testimonial.php and archive-testimonial.php, when added to the current theme directory, allow customization of the display of our new post type. Note that, at least in our case, to get the new single and archive files to start working, we had to change the permalink structure, save, view a newly-created testimonial in the alternate permalink structure, change permalink structure back to our original settings, and view again.

And voilĂ : a semantically-appropriate method to store, manage, and display client testimonials on our site.

Posted In

Share

Share on Facebook Share on Twitter

About the Author

Brian Hoke is Principal of Bentley Hoke, a web development, design, and marketing consultancy in Syracuse, New York.