While I was doing some customization on my website’s WordPress template, I stumble upon the new feature “Testimonial Shortcode” added to every theme developed by EleganthTheme. I was fascinated with this feature but one thing I notice was that it lacks of flexibility, every time you want to add an item you have to edit the page or post where your shortcode resided.

Note that this tutorial is intended for WordPress audience who uses the ElegantTheme template and has intermediate knowledge in PHP and WordPress programming.

Now we will take a glimpse on what is Shortcode and Custom Post Type.

shortcode is a WordPress-specific code that lets you do nifty things with very little effort. Shortcodes can embed files or create objects that would normally require lots of complicated, ugly code in just one line. Shortcode = shortcut.

A custom post type is a type of content you want to be added to WordPress post database, basically WordPress has built-in post type such as page and post, when creating a custom post type you can define your own name (e.g people, animals, etc.) , but of course the name should be related to the content you want to add to avoid confusions. In this tutorial we will make a post type called “Testimonial”

First, we will make a custom post type. I will not take detail discussion on this, you may visit complete details in TutPlus Tutorial. Here is the code for custom post type creation, this code is to be place in function.php file under your template directory:

/**-- Custom Post type Testimony-- **/
define('CUSTOM_POST_TYPE','testimony');

define('CUSTOM_MENU_TITLE',__('Testimony','YourTheme'));
define('CUSTOM_MENU_NAME',__('Testimony','YourTheme'));
define('CUSTOM_MENU_SIGULAR_NAME',__('Testimony','YourTheme'));
define('CUSTOM_MENU_ADD_NEW',__('Add Testimony','YourTheme'));
define('CUSTOM_MENU_ADD_NEW_ITEM',__('Add new testimony','YourTheme'));
define('CUSTOM_MENU_EDIT',__('Edit','YourTheme'));
define('CUSTOM_MENU_EDIT_ITEM',__('Edit testimony','YourTheme'));
define('CUSTOM_MENU_NEW',__('New testimony','YourTheme'));
define('CUSTOM_MENU_VIEW',__('View testimony','YourTheme'));
define('CUSTOM_MENU_SEARCH',__('Search testimony','YourTheme'));
define('CUSTOM_MENU_NOT_FOUND',__('No testimony created','YourTheme'));
define('CUSTOM_MENU_NOT_FOUND_TRASH',__('No testimony found in trash','YourTheme'));

$custom_post_type = CUSTOM_POST_TYPE;
register_post_type( "$custom_post_type",
array( 'label' => CUSTOM_MENU_TITLE,
'labels' => array( 'name' => CUSTOM_MENU_NAME,
'singular_name' => CUSTOM_MENU_SIGULAR_NAME,
'add_new' => CUSTOM_MENU_ADD_NEW,
'add_new_item' => CUSTOM_MENU_ADD_NEW_ITEM,
'edit' => CUSTOM_MENU_EDIT,
'edit_item' => CUSTOM_MENU_EDIT_ITEM,
'new_item' => CUSTOM_MENU_NEW,
'view_item' => CUSTOM_MENU_VIEW,
'search_items' => CUSTOM_MENU_SEARCH,
'not_found' => CUSTOM_MENU_NOT_FOUND,
'not_found_in_trash' => CUSTOM_MENU_NOT_FOUND_TRASH),
'public' => true,
'can_export' => true,
'show_ui' => true, // UI in admin panel
'_builtin' => false, // It's a custom post type, not built in
'_edit_link' => 'post.php?post=%d',
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array("slug" => "$custom_post_type"), // Permalinks
'query_var' => "$custom_post_type", // This goes to the WP_Query schema
'supports' => array( 'title',
'author',
'excerpt',
'thumbnail',
'comments',
'editor',
'trackbacks',
'revisions',
'page-attributes') ,
'menu_position' => 7,
'show_in_nav_menus' => true
)
);

/** Facilities category EOF **/
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 $content;
break;
/* Just break out of the switch statement for everything else. */
default :
break;
}
}
add_filter( 'manage_edit_testimony_sortable_columns', 'Custom_testimony_sortable_columns' );
function Custom_testimony_sortable_columns( $columns ) {
$columns['content'] = 'Testimony';
return $columns;
}

Next we will create a shortcode name “custom_testimonial”

add_shortcode('custom_testimonial', 'custom_testimonial_generator');
function custom_testimonial_generator() { $p1 = new WP_Query('post_type=testimony' ); while ( $p1->have_posts() ) : $p1->the_post(); $content = et_content_helper(get_the_content()); $author = get_the_title(); $company = get_post_meta(get_the_ID(),'t_company',true); $image_url = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'thumbnail' ); $image = $image_url[0]; echo do_shortcode("[tstimonial author='".$author."' company='".$company."' image='".$image."']".$content."[/tstimonial]"); endwhile; wp_reset_postdata(); }

Ok we’re done, the code above query the database and them apply eleganttheme’s short code.

Finally, you can post include “[custom_testimonial]” in your post or page, that’s all.

Hope you find this tutorial helpful.

 

Disclosure: Some of the links in this post are "affiliate links". We may receive a commission for purchases made through these links.

One thought on “Utilizing Eleganttheme’s Testimonial Shortcode

  1. Moncler Jackets

    Some really great blog posts on this web site, thanks for contribution. “It is not often that someone comes along who is a true friend and a good writer.” by E. B. White.

Comments are closed.