While customizing my WordPress template, I came across an interesting new feature called the “Testimonial Shortcode” that comes with themes developed by ElegantThemes.
I was fascinated by this feature, but I noticed one limitation — it lacks flexibility. Every time you want to add a new testimonial, you have to manually edit the page or post that contains the shortcode.
This tutorial is intended for WordPress developers and intermediate users who are using an ElegantThemes template and have some knowledge of PHP and WordPress programming.
What Are Shortcodes and Custom Post Types?
Before we dive into coding, let’s review what shortcodes and custom post types are.
What Is a Shortcode?
A shortcode is a WordPress-specific tag that lets you perform powerful functions with minimal code.
Shortcodes can embed files or create objects that would otherwise require complex programming — all in one line of code.
Example:
<code>[shortcode_name]</code>
In short: shortcode = shortcut.
What Is a Custom Post Type?
A custom post type allows you to add new kinds of content to your WordPress site beyond the default “posts” and “pages.”
For instance, you might create custom post types for “Testimonials,” “Portfolio Items,” or “Events.”
In this tutorial, we’ll create a custom post type called “Testimony.”
Step 1: Register a Custom Post Type
First, we’ll create the custom post type.
You can place the following code in your functions.php file inside your theme directory.
/**
* Register Custom Post Type: Testimony
*/
function register_testimony_post_type() {
$labels = array(
'name' => __( 'Testimony', 'yourtheme' ),
'singular_name' => __( 'Testimony', 'yourtheme' ),
'add_new' => __( 'Add Testimony', 'yourtheme' ),
'add_new_item' => __( 'Add New Testimony', 'yourtheme' ),
'edit_item' => __( 'Edit Testimony', 'yourtheme' ),
'new_item' => __( 'New Testimony', 'yourtheme' ),
'view_item' => __( 'View Testimony', 'yourtheme' ),
'search_items' => __( 'Search Testimony', 'yourtheme' ),
'not_found' => __( 'No testimony created', 'yourtheme' ),
'not_found_in_trash' => __( 'No testimony found in trash', 'yourtheme' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'can_export' => true,
'_builtin' => false,
'_edit_link' => 'post.php?post=%d',
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array(
'slug' => 'testimony',
),
'query_var' => 'testimony',
'supports' => array(
'title',
'editor',
'author',
'excerpt',
'thumbnail',
'comments',
'trackbacks',
'revisions',
'page-attributes',
),
'menu_position' => 7,
'show_in_nav_menus' => true,
);
register_post_type( 'testimony', $args );
}
add_action( 'init', 'register_testimony_post_type' );Step 2: Customize the Admin Columns
To make the “Testimony” post type easier to manage in the WordPress admin panel, add this code to modify the columns.
add_filter( 'manage_edit-testimony_columns', 'custom_edit_testimony_columns' );
function custom_edit_testimony_columns( $columns ) {
$columns = array(
'cb' => '',
'title' => 'Author Name',
'content' => 'Testimony',
);
return $columns;
}
add_action( 'manage_testimony_posts_custom_column', 'custom_manage_testimony_columns', 10, 2 );
function custom_manage_testimony_columns( $column, $post_id ) {
global $post;
switch ( $column ) {
case 'content':
$content = et_content_helper( $post->post_content );
echo esc_html( $content );
break;
default:
break;
}
}
add_filter( 'manage_edit_testimony_sortable_columns', 'custom_testimony_sortable_columns' );
function custom_testimony_sortable_columns( $columns ) {
$columns['content'] = 'Testimony';
return $columns;
}
Step 3: Create the Shortcode
Now, we’ll create a shortcode called “custom_testimonial” that dynamically retrieves all “Testimony” posts and outputs them using ElegantThemes’ built-in testimonial shortcode.
Add the following code to your functions.php file:
<?php
/**
* Generate custom testimonials shortcode output
*/
function custom_testimonial_generator() {
$testimonial_query = new WP_Query(
array(
'post_type' => 'testimony',
'posts_per_page' => -1, // optional: get all testimonials
)
);
if ( $testimonial_query->have_posts() ) {
while ( $testimonial_query->have_posts() ) {
$testimonial_query->the_post();
$content = et_content_helper( get_the_content() );
$author = get_the_title();
$company = get_post_meta( get_the_ID(), 't_company', true );
$thumbnail_id = get_post_thumbnail_id( get_the_ID() );
$image_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail' );
$image = ! empty( $image_url[0] ) ? $image_url[0] : '';
echo do_shortcode(
sprintf(
'[tstimonial author="%s" company="%s" image="%s"]%s[/tstimonial]',
esc_attr( $author ),
esc_attr( $company ),
esc_url( $image ),
$content
)
);
}
}
wp_reset_postdata();
}Step 4: Use the Shortcode in a Page or Post
Finally, you can display all your testimonials on any page or post by inserting the following shortcode:
<code>[custom_testimonial]</code>
That’s it!
This shortcode will automatically display all your custom testimonials using ElegantThemes’ testimonial format.
Conclusion
By creating a custom post type and a custom shortcode, you can easily manage and display testimonials without manually editing each page.
This approach gives you more flexibility, organization, and control over your website’s testimonial section.
Hope you find this tutorial helpful and that it helps you make better use of ElegantThemes’ testimonial shortcode feature.