Including a PHP Template File in Divi Builder

Written by Dan Mossop

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.

Run PHP Code Directly in your Divi Layouts

Unlock endless customization, automation, and dynamic functionality by seamlessly adding PHP code to your Divi pages and posts with the Divi PHP Code Module. Style, preview, and debug your PHP creations directly in the visual builder with robust error handling and enhanced security.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

2 Comments

  1. Hi Dan,

    Can a PHP template also be included in the DIVI Theme Builder in this way? As you know, the DIVI theme builder will override the PHP template that WordPress would use for a Custom Post Type. I'm looking for a simple way to keep using the front-end rendering that this PHP template would provide, without completely reconstructing it in the DIVI theme builder.

    Thank you in advance for your answer!

    Best,
    Ilse

    Reply
    • Hi Ilse, the PHP code module will work in the theme builder as well, so you should be able to include templates in the same way there – the example shown in the post should, I believe, work in the theme builder too. Please feel free to give me a shout if you have any questions while setting it up. Thanks!

      Reply

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 *.

We may earn a commission when you visit links on our website.

Latest Posts

Hide the Page Title in the Hello Elementor Theme

Hiding the page title in the Hello Elementor theme allows you to create a cleaner and more customized page layout, free from default headings that may not fit your design. This is particularly useful for landing pages or custom content layouts where the default page...

How to Hide a Divi Module When Scrolling Up or Down

In this quick tutorial, I’ll show you how to hide a Divi module when scrolling in a specific direction (up or down), so that the module only shows when you want it to. This is especially useful when using Divi’s Scroll Effects feature and you have a effect you want to...

Fade a Divi Image Module Edge into the Background

Want to create a stylish fade effect on your Divi image module—where one side fades smoothly into the background? With a bit of CSS, you can make any edge (or corner) of the image fade out: top, bottom, left, right, or even diagonally.Fade a Divi Image Module Edge...

Hide the Header and Footer in the Hello Elementor Theme

Removing the default header and footer from your Hello Elementor theme allows for a streamlined and distraction-free website design. This is especially useful when creating unique landing pages, full-width layouts, or custom headers and footers with a page builder. In...

Setting up the Divi Password Box Module

Setting up password protection on a page can help you control access to sensitive or private content in WordPress, allowing only authorized visitors to view certain sections. With the Divi Password Box module, you can replace the plain Divi password form with a fully...

Random Posts