Including a PHP Template File in Divi Builder

|

The Divi PHP Code Module lets you easily add PHP code anywhere in your Divi Builder pages. One of the many things you can do with it is insert existing PHP templates into your layout. Here's an example:

Suppose you have an existing PHP template file called "testimonials-section.php", located in the "templates" folder in your main WordPress installation folder, and that it contains the following template (with a mix of HTML and PHP code):

<blockquote class="testimonial">
	<span class="text">"<?php esc_html_e($text); ?>"</span>
	by
	<span class="author"><?php esc_html_e($name); ?></span>
</blockquote>

You can easily display this template anywhere in your Divi builder layout using the Divi PHP Code module. To do so, simply place the following PHP code in the PHP Code module:

$name = "Dan";
$text = "This is great!";
include(ABSPATH.'templates/testimonials-section.php');

Like so:

Note that the "ABSPATH" constant refers to the location of the main WordPress folder. You may need to modify the path to the file if it is placed elsewhere on your server.

Now the PHP Code module will load and run the template, passing the $name and $text variables to it. The result is that the template will be displayed, with the values filled in, e.g.:

If you like, you can now copy the PHP Code module and pass in different values for the variables in each copy of the module.

This post may contain referral links which may earn a commission for this site

Divi Booster

Hundreds of new features for Divi
in one easy-to-use plugin

0 Comments

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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