Last updated on October 18, 2023

Divi testimonial module

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Use Divi's testimonial module with code.

The Divi testimonial module is a popular feature in the Divi theme for WordPress. It allows you to easily display testimonials on your website in a visually appealing way. Here’s an example of how you can create a custom Divi testimonial module using the WordPress coding standards:

function wpsnippets_divi_testimonial_module( $args ) {
    ob_start();

    // Query testimonials
    $testimonials = new WP_Query( array(
        'post_type'      => 'testimonial',
        'posts_per_page' => $args['count'],
        'orderby'        => 'date',
        'order'          => 'DESC',
    ) );

    // Check if there are testimonials
    if ( $testimonials->have_posts() ) {
        ?>
        <div class="testimonial-module">
            <?php while ( $testimonials->have_posts() ) : $testimonials->the_post(); ?>
                <div class="testimonial">
                    <div class="testimonial-content"><?php the_content(); ?></div>
                    <div class="testimonial-author"><?php the_title(); ?></div>
                </div>
            <?php endwhile; ?>
        </div>
        <?php
    }

    wp_reset_postdata();

    return ob_get_clean();
}
add_shortcode( 'divi_testimonial_module', 'wpsnippets_divi_testimonial_module' );

This code snippet creates a custom shortcode [divi_testimonial_module] that can be used to display testimonials on any page or post. It uses the WP_Query class to query testimonials from the testimonial post type and then loops through the testimonials to display their content and author.

To use this code snippet, simply add it to your theme’s functions.php file or a custom plugin file. You can then use the [divi_testimonial_module] shortcode in your content to display the testimonials.

Examples

Example 1: Creating a Custom Testimonial Module in Divi

This example demonstrates how to create a custom testimonial module in Divi using the et_pb_module_shortcode() function.

function wpsnippets_custom_testimonial_module() {
    if ( class_exists( 'ET_Builder_Module' ) ) {
        class ET_Builder_Module_Custom_Testimonial extends ET_Builder_Module {
            public $slug       = 'et_pb_custom_testimonial';
            public $vb_support = 'on';

            protected $module_credits = array(
                'module_uri' => '',
                'author'     => '',
                'author_uri' => '',
            );

            public function init() {
                $this->name = esc_html__( 'Custom Testimonial', 'et_builder' );
                $this->type = 'child';
                $this->child_title_var = 'name';
                $this->advanced_setting_title_text = esc_html__( 'New Testimonial', 'et_builder' );
                $this->settings_text = esc_html__( 'Testimonial Settings', 'et_builder' );
            }

            public function get_fields() {
                return array(
                    'name' => array(
                        'label'           => esc_html__( 'Name', 'et_builder' ),
                        'type'            => 'text',
                        'option_category' => 'basic_option',
                        'description'     => esc_html__( 'Enter the name of the person giving the testimonial.', 'et_builder' ),
                        'toggle_slug'     => 'main_content',
                    ),
                    'content' => array(
                        'label'           => esc_html__( 'Content', 'et_builder' ),
                        'type'            => 'textarea',
                        'option_category' => 'basic_option',
                        'description'     => esc_html__( 'Enter the testimonial content.', 'et_builder' ),
                        'toggle_slug'     => 'main_content',
                    ),
                );
            }

            public function shortcode_callback( $atts, $content = null, $function_name ) {
                $name    = $this->shortcode_atts['name'];
                $content = $this->shortcode_content;

                ob_start();
                ?>
                <div class="testimonial">
                    <h3><?php echo esc_html( $name ); ?></h3>
                    <p><?php echo esc_html( $content ); ?></p>
                </div>
                <?php
                return ob_get_clean();
            }
        }

        new ET_Builder_Module_Custom_Testimonial;
    }
}
add_action( 'wp_loaded', 'wpsnippets_custom_testimonial_module' );

This code example demonstrates how to create a custom testimonial module in Divi. The ET_Builder_Module_Custom_Testimonial class extends the ET_Builder_Module class and defines the module’s settings and fields. The shortcode_callback() function is responsible for rendering the module’s output. The custom testimonial module can be used in the Divi Builder by adding the [et_pb_custom_testimonial] shortcode.

Example 2: Adding Custom Testimonial Module to Divi Builder

This example demonstrates how to add the custom testimonial module to the Divi Builder using the et_builder_register_module() function.

function wpsnippets_add_custom_testimonial_module() {
    if ( class_exists( 'ET_Builder_Module' ) ) {
        class ET_Builder_Module_Custom_Testimonial extends ET_Builder_Module {
            // Module code here...
        }

        et_builder_register_module( ET_Builder_Module_Custom_Testimonial::get_module_slug() );
    }
}
add_action( 'et_builder_ready', 'wpsnippets_add_custom_testimonial_module' );

This code example shows how to add the custom testimonial module to the Divi Builder. The et_builder_register_module() function is used to register the custom module, making it available for use in the Divi Builder.

Example 3: Customizing Testimonial Module Output

This example demonstrates how to customize the output of the testimonial module by modifying the shortcode_callback() function.

public function shortcode_callback( $atts, $content = null, $function_name ) {
    $name    = $this->shortcode_atts['name'];
    $content = $this->shortcode_content;

    ob_start();
    ?>
    <div class="testimonial">
        <h3><?php echo esc_html( $name ); ?></h3>
        <blockquote><?php echo wpautop( esc_html( $content ) ); ?></blockquote>
    </div>
    <?php
    return ob_get_clean();
}

This code example shows how to customize the output of the testimonial module by modifying the shortcode_callback() function. In this example, the testimonial content is wrapped in a <blockquote> element and passed through the wpautop() function to automatically add paragraph tags.

Last updated on October 18, 2023. Originally posted on November 5, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *