Creating a Testimonials Plugin for WordPress

Posted on 18th June 2023

WordPress is a great content management system that enables you to create a wide variety of websites and plugins are a big part of what makes it so great. In this article, we’ll be creating a testimonials plugin for WordPress. This plugin will enable you to easily create and manage testimonials on your website.

Creating the Plugin

Creating a WordPress plugin is a fairly straightforward process. First, you’ll need to create a new directory for your plugin. This can be done by going to the wp-content/plugins directory and creating a new directory called testimonials-plugin.

Next, you’ll need to create a file called testimonials-plugin.php in this new directory. This file will contain the code for your plugin.

Finally, you’ll need to activate your plugin by going to the Plugins page in the WordPress admin area and selecting your plugin from the list.

Creating the Testimonials Custom Post Type

The first thing we need to do in our plugin is to create a custom post type for testimonials. This can be done by adding the following code to your testimonials-plugin.php file:

array(

‘name’ => __( ‘Testimonials’ ),

‘singular_name’ => __( ‘Testimonial’ )

),

‘public’ => true,

‘has_archive’ => true,

)

);

}

add_action( ‘init’, ‘create_testimonials_post_type’ );

?>

This code registers a new post type called testimonial. It also defines some labels for the post type, such as the name and singular name. Finally, it sets the public and has_archive parameters to true so that our testimonials will be publicly accessible and will be indexed by WordPress.

Creating the Testimonials Custom Fields

Next, we need to create some custom fields for our testimonials. We’ll need a field for the testimonial author’s name and a field for the testimonial itself.

We can add these fields by adding the following code to our plugin:

ID, ‘_testimonial_author’, true );

echo ‘

_e( “Author’s Name”, ‘textdomain’ );

echo ‘ ‘;

echo ”;

}

function testimonial_content_meta_box( $post ) {

wp_nonce_field( plugin_basename( __FILE__ ), ‘testimonial_nonce’ );

$testimonial_content = get_post_meta( $post->ID, ‘_testimonial_content’, true );

echo ‘

_e( “Testimonial”, ‘textdomain’ );

echo ‘ ‘;

echo ‘‘;

}

?>

This code adds two meta boxes to the testimonial post type – one for the author’s name and one for the testimonial content.

The code then defines two callback functions – one for each meta box. These functions output the HTML for the meta boxes.

Finally, the code saves the data from these meta boxes when the testimonial post is saved.

Creating the Testimonials Shortcode

Now that we have our testimonials custom post type and custom fields set up, we can create a shortcode to output our testimonials on the front-end of our website.

We can do this by adding the following code to our plugin:

-1,

), $atts ) );

$args = array(

‘post_type’ => ‘testimonial’,

‘posts_per_page’ => $limit,

);

$testimonials = new WP_Query( $args );

if ( $testimonials->have_posts() ) {

$output = ‘

‘;

while ( $testimonials->have_posts() ) {

$testimonials->the_post();

$testimonial_author = get_post_meta( get_the_ID(), ‘_testimonial_author’, true );

$testimonial_content = get_post_meta( get_the_ID(), ‘_testimonial_content’, true );

$output .= ‘

‘;

$output .= ‘

‘ . esc_html( $testimonial_author ) . ‘

‘;

$output .= ‘

‘ . wpautop( esc_html( $testimonial_content ) ) . ‘

‘;

$output .= ‘

‘;

}

$output .= ‘

‘;

wp_reset_postdata();

return $output;

}

add_shortcode( ‘testimonials’, ‘testimonials_shortcode’ );

?>

This code defines a testimonials shortcode. The shortcode takes an optional limit parameter which specifies the number of testimonials to output. If no limit is specified, all testimonials will be output.

The code then defines a WP_Query to get the testimonials. It loops through the testimonials and outputs the author and content for each one.

Finally, the code returns the output.

Conclusion

In this article, we’ve seen how to create a testimonials plugin for WordPress. This plugin enables you to easily create and manage testimonials on your website.

Once you have the plugin installed and activated, you will need to create a new post or page in WordPress and enter the shortcode [testimonials] into the content editor. This will output the testimonials on the front-end of your website.

If you want to display the testimonials in a widget, you can use the [testimonials] shortcode in the Text widget.

To style the testimonials, you will need to edit the CSS file in the plugin folder. The CSS file is located in the “css” folder and is named “testimonials.css”.

To change the color of the testimonials, you will need to edit the “color” property in the “.testimonials” class. For example, to change the color to blue, you would add the following code to the CSS file:

.testimonials {
color: blue;
}

You can also change the font-size, font-family, and other properties in the same way.

If you want to display the testimonials in a different order, you can edit the “orderby” and “order” parameters in the [testimonials] shortcode. For example, to display the testimonials in reverse chronological order, you would use the following shortcode:

[testimonials orderby=”date” order=”DESC”]

You can also use the “orderby” parameter to order the testimonials by the “title” or “ID” field.

If you only want to display certain testimonials, you can use the “id” parameter to specify the IDs of the testimonials you want to display. For example, to only display testimonials with the IDs 1, 2, and 3, you would use the following shortcode:

[testimonials id=”1,2,3″]

You can also use the “category” parameter to only display testimonials from a certain category. For example, to only display testimonials from the “Testimonials” category, you would use the following shortcode:

[testimonials category=”testimonials”]

You can also use the “limit” parameter to specify the number of testimonials to display. For example, to only display 3 testimonials, you would use the following shortcode:

[testimonials limit=”3″]

You can use any combination of these parameters to display the testimonials exactly how you want.

That’s all there is to creating a testimonials plugin for WordPress. With this plugin, you can easily display testimonials on your website in any way you want.