Divi PHP Code Module

The Divi PHP Code module by Divi Booster lets you add and run PHP code anywhere in your Divi pages. With it, you can:

  • Run PHP code anywhere in your Divi pages
  • Preview and debug the code in the visual builder
  • Style the output using the Divi design settings

Lifetime license. Nothing more to pay.
Automatic updates. 90 day money back guarantee.

Divi Show / Hide Button Module
WP 6.5.2 Compatible

Easily Add PHP Code to your Divi Pages

Introducing the Divi PHP Code Module, the ultimate tool for harnessing the power of PHP right within the Divi Builder. With this easy-to-use Divi module, you can seamlessly add and run PHP code directly in your Divi pages and posts, providing endless possibilities for customization, automation, and dynamic functionality.

Adds a PHP Code Module

With Divi PHP Code Module you get a new "PHP Code" module in the Divi Builder that gives you everything you need to quickly and easily add PHP code into your Divi pages. 

Unlike the native Divi "Code" module, which only allows static HTML and JavaScript, the Divi PHP Code module allows you to go further and dynamically generate content using using PHP. 

Divi PHP Code module makes adding PHP code simple – more messing about with child themes, or trying to wrap your PHP code in shortcodes.

Enter your PHP Code… and you're done

 

Simply insert the PHP Code module into your page, add PHP code in the module's settings, and then when the page is viewed the PHP code will run and its output will be displayed where the module is on the page… like this:

An Example PHP Code Module

Hi, I am a Divi PHP Code Module!

Test your code in the Visual Builder

You can quickly test and run your code right from in the visual builder. Simply click on the green "Play" button beside the PHP Code box to run your PHP code and see how it will look in the final page. 

Built-in Debugging and Error Handling

With the built-in Debug option, you have complete control over error display. Easily toggle the display of errors on or off, allowing you to efficiently troubleshoot and resolve any issues that may arise. Divi PHP Code Module incorporates robust error handling, ensuring that even if an error occurs, your website remains intact and functional.

No more broken layouts, blank screens, or frustrated visitors – just smooth and reliable performance.

Designed for Security

The Divi PHP Code Module has been carefully designed with security in mind, ensuring that your PHP code runs smoothly while protecting your website. With Divi Role Editor integration and cryptographic integrity checking, the module is designed to ensure that you have full control over who gets to run code on your site.

Examples

With the power of PHP in a Divi Builder module, your capabilities are essentially limitless. But to get you started thinking about how you can use the Divi PHP Code Module in your own projects, here are a few small examples:

Creating Tables

Stop manually editing tables! Instead, load the data directly the original source, and display it as a HTML table, formatted exactly how you like it.

Sample Table

NameAgeCity
John 25 New York
Sarah 32 London
Michael 28 Paris
Emily 30 Sydney

Text Effects

Make your text and headings stand out! With the power of PHP you can easily transform your text in ways that are difficult with CSS alone.

Text Gradient Effect

Welcome to the Digital World!

Display Dynamic Information

Easily display information from your database or other sources (e.g. APIs). 

WordPress Stats

Dan has written 555 posts and 2860 comments on this site (and needs a rest).

Creating Forms

Add forms to your page with a mix of HTML and PHP code. 

My Custom Form

FAQs

How is this different from the Divi's own Code Module?

The built-in Divi Code module and the Divi PHP Code module by Divi Booster serve a similar purpose of allowing custom code to be added to Divi layouts. However, there is a key difference in the languages they support.

The built-in Divi Code module can only handle static HTML and JavaScript code.

Divi PHP Code module, on the other hand, also allows PHP code to be added. PHP is a server-side scripting language that enables dynamic content generation. As such, it allows you to execute custom server-side logic and generate dynamic content for your layouts, something that can't be achieved through the standard Divi Code module..

By supporting PHP, the Divi PHP Code module gives you more flexibility and power. It opens up possibilities for integrating with external systems, fetching and displaying dynamic data, performing advanced calculations, etc.

So is any PHP code I add secure?

No, not necessarily. The PHP Code module aims to provide a secure environment for adding PHP code to the Divi Builder, but can't guarantee the security of any code you add.

The Divi PHP Code module is designed to only run PHP code that has been added through the visual builder interface – when code is added in this way, a keyed cryptographic hash is added which is checked before the code is actually run. Additionally the permissions set in the Divi Role Editor are checked to ensure that the user has been granted access to use the module, and by default only the administrator is granted permission – other user roles must be given specific permission to use the module. Together, these mechanisms seek to prevent anyone adding or altering code without permission.

But what the PHP code you add actually does is entirely up to you – you can add secure code or insecure code, and the responsibility for that rests with you. The functionality of added code isn't restricted by the module in any way, so you are free to use the full power and functionality of PHP. But this means you also need to be aware of the security implications of the code you add. While you could add code to let anyone run any command on your server, or to delete your database, doing so would probably be a very bad idea. So follow secure coding principles when adding code. For example, make sure you understand what the code does, avoid accepting user input (or perform whitelist validation on it, if you must), and appropriately escape data before it is used. 

With the Divi PHP Code module's built-in security mechanisms and good security practices on your part, there shouldn't be any issues. I do, of course, recommend taking regular backups as a standard and sensible way to further protect your site and data.

 

When is the PHP code run?

The PHP code is run in the module's render() function. This means that it runs at the point in the page where the module output is being generated. As such, it runs after WordPress, Divi and any installed plugins have been loaded, which means that your code can take advantage of the functions / classes that they add. There are some things that PHP code added via the module will be too late to affect – for example, it won't be able to add filters / actions to hooks which are run before the page generation begins. 

Get Divi PHP Code Module

Increase your productivity. Build better websites. Start today with Divi PHP Code Module. 

Divi PHP Code Module

Automatic updates. No recurring fees.
90 day money back guarantee.

Got a Question?

12 Comments

  1. I've REALLY been trying to use this thing, and it just seems to not work worth a damn. I've been a senior PHP engineer for 15 years. The debugging is virtually useless. I should be able to just require_once an already-working page with what I need, but that doesn't work. I've tried declaring very small classes, and it doesn't work. I've tried just $zippy = array(); and THAT doesn't work. I can hit the database with global $wpdb->get_row() and see the results with a print_r.
    But, I can't DO anything with that. Later, I need to $wpdb->get_results and use the resulting rows – NOTHING WORKS.
    I cannot even put at the end of the code ( kind of ), but this thing seems to be severely crippled. Any help you can give me would be appreciated.

    Reply
    • Hello Neil,

      I'm sorry to hear about the difficulties you've been experiencing with the Divi PHP Code Module. There's certainly no intentional limitation in its functionality, and I'd like to help clear things up and get it working for you.

      Let me provide a bit of context on how the code within the PHP Code Module is executed, which might shed some light on the issues you're encountering. The code is run using eval() within the render() method of the module's class, which scopes it to that method. Communication between instances of the module might be affected by this as variables, functions, classes, etc, declared in one instance aren't directly accessible in another. To share state and objects, you might need to store and retrieve them via globals or the database.

      Another point to consider is that the code's execution time occurs later in the page loading process, meaning early page elements like the <head> cannot be modified by the PHP Code Module's output.

      If eval() has been disabled by your web host, that would certainly cause problems, but since you mentioned being able to use print_r(), it seems eval() is operational. For a double-check, a test call to eval() in your functions.php could confirm if it's available.

      For debugging, ensure that you have "Enable Debugging" activated in the module's options. This will display specific error messages rather than a generic one. I'm also enhancing the debugging with a stack trace in the upcoming v1.0.6 update, which should hopefully help in pinpointing your issues. This update will also restore the green "play" button (it was blocked by the Divi AI button) that lets you run / preview the code in the visual builder. I'll update here as soon as the update is available.

      Now, let's tackle the code troubles you're having. Here are three snippets that use the types of constructs you've mentioned and which work on my test site:

      Retrieve titles of the last 5 posts:

      
      global $wpdb;
      
      $zippy = array();
       
      // Query to retrieve the 5 most recent published post titles
      $recent_posts = $wpdb->get_results( 
          "SELECT post_title 
          FROM $wpdb->posts
          WHERE post_status = 'publish' 
          AND post_type = 'post'
          ORDER BY post_date DESC 
          LIMIT 5", 
          OBJECT
      );
      
      // Check if we have results
      if ( !empty($recent_posts) ) {
          // Loop through the results and place the titles into the zippy array
          foreach ( $recent_posts as $post ) {
              $zippy[] = $post->post_title;
          }
      }
      
      // Check if the zippy array is not empty and print the titles as an HTML list
      if ( !empty($zippy) ) {
          echo '<ul>';
          foreach ( $zippy as $title ) {
              echo '<li>' . esc_html($title) . '</li>';
          }
          echo '</ul>';
      }
      

      Declare and use a simple class:

      
      class Car {
      
          public $color;
          
          public function __construct($color = 'red') {
              $this->color = $color;
          }
      
          public function getColor() {
              return $this->color;
          }
      }
      
      $myCar = new Car('blue'); 
      echo "My car is " . $myCar->getColor(); // Outputs: My car is blue
      

      Require an existing file (outputs the Divi social icons – Facebook, X, etc – as long as they are enabled in the Divi Theme Options):

      
      require_once get_template_directory().'/includes/social_icons.php';
      

      Could you test these examples in your environment and let me know the result? If you encounter errors, please let me know what errors are returned. Also if you are able to share any of the example scripts you've been trying to get working I'll test them out on my end.

      Thanks for your patience.

      Reply
  2. Hello, I am planning to create a multipage WP wordpress website, made with divi. It has many end product pages, which can be navigated via different menus. I have created several set of menus for ease of navigating. As an example, domain/page-endproduct should be reached by in domain/menu-1 and domain/menu-2. But when navigating the domain/page-endproduct the user should get the original menu which he started whether it is menu-1 or menu-2. so the page's menu should be dynamic depending on the users previous navigation page. can I do it with this? how?

    Reply
    • Hi Rajith,

      I've just published a post that I think may be along the lines of the solution you're looking for. It shows you how to dynamically change the WordPress menu displayed in the Divi Menu module, based on a menu ID passed via the page URL. This allows you to show different menus depending on the user's navigational path to the page. For your specific end product URLs, once the necessary PHP code is in place, you'll want to append a query parameter to them. For example, if your end product URL is domain/page-endproduct, you could link to it with domain/page-endproduct?menu=123 where 123 is the menu ID you want to show.

      Read the full post here for a detailed implementation:

      Dynamically Changing the Divi Menu Module Menu

      The PHP code can be added using the Divi PHP Code module (as described in the post), though in some cases it may make more sense to apply the PHP globally (also discussed).

      I hope it helps, but if it doesn't fully meet your needs or if you have other scenarios in mind, please let me know. Thanks!

      Reply
  3. Hi

    I just bought the Agency Bundle. Thanks for the awesome special.

    I'm wondering if the PHP module is similar to the Cope Snippets WordPress plugin. I understand that it is added as a module to a single page. I need to enter PHP to change things site-wide. For example, on my blog posts, the phrase '75 comments' or '36 comments' is tagged as a H1 header. I need to enter code to make this 'number of comments' into a . I don't use a DIVI blog post template, just the standard WordPress one. In other words I can't add a PHP module to a template and have it work site-side. Is it possible to do this with the PHP module or is it limited to a single page.

    Thanks

    Reply
    • You're very welcome, John.

      The PHP module only runs the PHP code on the pages it appears on (and at the point in the page that it appears). As such, if you want it to apply site-wide you would need to add it to a template that is used on every page. If you aren't using a blog post template, perhaps you have a global header? Putting it in there should work.

      More generally, I'd suggest that if your comments are added using a comments module, then place the PHP code module in the same place as the comments module.

      Otherwise, I'd suggest using the Code Snippets plugin for this task. Essentially the standard WordPress comments are a globally available feature and so it makes sense to use globally applied PHP code to modify them. This is what the Code Snippets plugin is good for – globally applied PHP code. The PHP Code module is best suited for when you need more targeted PHP Code (i.e. that runs in a particular page or template).

      I hope that helps / makes sense :)

      Reply
  4. Hello,

    I would like to acquire the php code module but I am not sure If it fulfills my needs.
    I have a text file somewhere in a folder in WordPress installation Root folder. This text file contains some information on each line and I want display randomly one line of this file.
    I get random line from text file with php code but now I cannot pass it to divi text module using a shortcode as divi does not display anything.
    With php code module, can I obtain that?

    Thank you!

    Reply
    • Hi Marius,

      The Divi PHP Code Module is designed for things like this, and should be able to do what you need.

      A minimal example of the code to read a random file from a file called "faqs.txt" in the WP root directory, would be:

      $path = ABSPATH.'faqs.txt';
      $faqs = file($path);
      $faq_num = array_rand($faqs);
      echo $faqs[$faq_num];
      

      (Note that this example doesn't do any handling of errors, empty lines, etc.)

      Alternatively, if you already have the function for retrieving the lines defined, you could just call from the module with, e.g.:

      echo get_random_faq();
      

      (replacing get_random_faq with the name of your function)

      Or if you could even run your shortcode with, e.g.:

      echo do_shortcode('[my_random_faq]');
      

      (again, replacing my_random_faq with your shortcode.

      That said, the Divi Text module should process your shortcode, so if you like, please feel free to send through a copy of your PHP code for adding the shortcode and I'll take a look and see if I can see anything that would be preventing it from working.

      I hope that helps!

      Reply
  5. I see the special expires today. Thus I need to rush. The best plugin I ever bought was the divi booster. With this php module (I think it can open up a whole new world) I might be able to write a payroll system? i.e I see your sample with the csv code. But will I be able to create database tables. I'm really old school and practically ancient. My language of choice (don't laugh) is Delphi 5 in which I regard myself an expert and prior to that (now don't burst out laughing) was clipper.

    I get the impression new languages seems to go back to the early 80's when we still used assembly. I suspect AI will go straight for machine code, and not bother with languages as we know them.

    My question. Will I be able to manipulate real database tables?

    Reply
    • Hey Theo,

      I'm really glad you like Divi Booster :)

      Please don't rush – if you do miss the deadline just send me a message and I'll ensure you get the discount.

      Yes, you can absolutely create database tables. You have the full PHP functionality at your disposal so you can use the PHP sql functions. You also have WordPress's database functionality available to you, i.e $wpdb.

      So to list the current database tables, you could place this in the Divi PHP Code module:

      global $wpdb;
      print_r($wpdb->tables);
      

      And to actually create a database table you could paste in something like this:

      global $wpdb;
      
      $table_name = $wpdb->prefix . "my_table"; 
      
      $charset_collate = $wpdb->get_charset_collate();
      
      $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
        name tinytext NOT NULL,
        text text NOT NULL,
        url varchar(55) DEFAULT '' NOT NULL,
        PRIMARY KEY  (id)
      ) $charset_collate;";
      
      require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
      dbDelta( $sql );
      

      This is very closely based on the code in this WordPress codex entry. I've tested it and can see it works.

      One thing I'd point out is that PHP code added via the Divi PHP Code module will be executed every time a page containing the module is viewed (unless caching is involved). So you may want to ensure your code can handle that without issues, or restrict modules that actually build the database to private pages.

      Good luck with the payroll system and give me a shout if you have any further questions about it.

      Thanks!

      P.S. I started out on C64 Basic so I'm not sure I can legitimately laugh at the Delphi / clipper developers of the world ;)

      Reply
  6. Hi,

    Your PHP module looks great.
    Can it be used to insert PHP template files into pages (eg something like /templates/testimonials-section.php)

    Reply
    • Thanks Donal. Yes, you can absolutely do this, with an include() statement. I've put up a post with an example for you:

      Including a PHP Template File in Divi Builder

      Basically if you include the template it will be loaded and executed. You can also pass variables into the template, as explained in the post.

      I hope that helps, but give me a shout if you have any questions about it. Thanks!

      and you can even pass variables to the template.

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