How to Add PHP Code to Divi – Code Module, Snippets & Child Themes

Written by Dan Mossop

Need to add PHP code to Divi? The right method depends on whether the code needs to output something inside a Divi layout, or run more generally across your site.

Divi’s built-in Code Module does not run PHP. It is intended for HTML, CSS, JavaScript, shortcodes and embeds. If you want PHP output to appear inside a specific Divi Builder layout, use a dedicated PHP module such as Divi PHP Code Module. If the PHP snippet should run across your site, use a code snippets plugin or a Divi child theme instead.

Quick Answer: Use Divi PHP Code Module for PHP output inside Divi Builder layouts. Use Code Snippets or a Divi child theme for sitewide PHP snippets. Avoid editing Divi’s parent theme functions.php file directly, as your changes can be overwritten when Divi updates.

    Which method should you use?

    Use Divi PHP Code Module when the PHP needs to output something in a specific place in your Divi layout, such as a page, post, Theme Builder template, global section or product layout.

    Use a code snippets plugin when the PHP should run more generally across your WordPress site, such as snippets that use hooks, filters, shortcodes, WooCommerce changes, admin customisations or login changes.

    Use a Divi child theme when you prefer to manage PHP in theme files, especially if you already use a child theme for other developer-level customisations.

    Add PHP Code Inside a Divi Builder Layout with Divi PHP Code Module

    Divi PHP Code Module adds a dedicated PHP Code module to the Divi Builder, allowing you to run PHP directly inside Divi pages, posts and layouts.

    This is useful when the PHP output needs to appear at a specific point in the layout, such as inside a page section, Theme Builder template, product layout or reusable Divi layout.

    Divi’s standard Code Module and Divi PHP Code Module solve different problems. The standard Code Module is useful for HTML, CSS, JavaScript, shortcodes and embeds, but it does not execute server-side PHP. Divi PHP Code Module adds a separate module designed specifically for running PHP in Divi layouts.

    Install and activate Divi PHP Code Module

    First, install and activate the Divi PHP Code Module plugin.

    Once activated, a new PHP Code module will be available in the Divi Builder alongside Divi’s other modules.

    Run PHP inside your Divi Builder layouts

    Divi PHP Code Module lets you place PHP output exactly where you need it in your Divi layouts, without editing Divi’s theme files.

    Add the PHP Code module to your layout

    Open the page, post or Theme Builder layout where you want the PHP output to appear.

    In the Divi Builder, click to add a new module, then search for the PHP Code module. Add it to the section, row or column where you want the PHP-generated output to display.

    Add your PHP code

    Open the PHP Code module settings and add your PHP code.

    When the page is viewed, the PHP code will run and its output will be displayed in the location of the module.

    This lets you treat PHP output like part of the Divi layout itself: position it visually, move it between sections, and save it in reusable layouts.

    Try some PHP Code Module examples

    Once you've added the PHP Code module to your layout, you can use it for a wide range of Divi-specific PHP tasks.

    For example, you can:

    For more details on the plugin itself, see the Divi PHP Code Module page.

    Add Sitewide PHP Code Using a Code Snippets Plugin

    A code snippets plugin lets you add and manage PHP snippets from the WordPress dashboard, without editing Divi’s theme files directly.

    This is usually a good option for PHP that should run across the site, such as snippets that add shortcodes, use WordPress actions or filters, modify WooCommerce behaviour, or change frontend / admin functionality.

    Install and activate a code snippets plugin

    Install and activate your preferred code snippets plugin. Once activated, you’ll be able to add PHP snippets from the WordPress dashboard instead of editing theme files directly.

    I use and recommend the free Code Snippets plugin for sitewide snippets.

    Create a new PHP snippet

    In your snippets plugin, add a new PHP snippet.

    Give it a clear name so you can recognise what it does later. This is especially useful if you end up adding several snippets over time.

    Paste in your PHP code

    Paste your PHP code into the snippet editor.

    Note that snippet plugins often expect you to enter only the PHP code itself, without an opening <?php tag, but the exact behaviour depends on the plugin.

    Save and activate the snippet

    Save the snippet and activate it.

    If the plugin gives you options for where the snippet should run, choose the most appropriate setting for the code. For example, some snippets may only need to run on the frontend, while others may need to run in the admin area or across the whole site.

    Learn more about using Code Snippets with Divi

    If you're using snippets regularly, it's worth keeping them organized and understanding when a snippet belongs in Code Snippets rather than in a child theme or Divi layout. 

    I've put together a separate guide on using the Code Snippets plugin with Divi, including when it's a good fit and how it compares with other ways of adding code.

    Add PHP Code to Divi Using a Child Theme

    A Divi child theme gives you an update-safe place to add custom theme files, including a child theme functions.php file for PHP snippets.

    This is useful for developer-managed sitewide customisations, such as registering shortcodes, adding filters, modifying WordPress behaviour, or keeping PHP customisations alongside other child theme changes.

    Avoid adding custom PHP directly to Divi’s parent theme functions.php file, typically located at /wp-content/themes/Divi/functions.php. Divi updates can replace parent theme files, causing your changes to be lost.

    If a tutorial says to "add this to functions.php," use your child theme’s functions.php file or a code snippets plugin instead.

    Create or install a Divi child theme

    If you are not already using a child theme, create and activate one before adding custom PHP to theme files.

    Your child theme should include its own functions.php file. This is where your custom PHP can be added.

    Open the child theme’s functions.php file

    Open your child theme’s functions.php file using your preferred method.

    This may be via SFTP, FTP, your hosting file manager, or a local development workflow.

    Avoid relying only on the WordPress Theme File Editor if you are adding PHP code, because a PHP error can prevent the site from loading and may also stop you from accessing the WordPress dashboard.

    Before making changes, make sure you have a recent backup and a way to restore the file if something goes wrong.

    Add your PHP code

    Add your PHP code to the child theme’s functions.php file.

    Add the code inside the existing PHP section of the file, usually after the opening <?php tag. If the snippet you copied includes its own opening <?php tag, check whether you need to remove it before pasting it into an existing functions.php file.

    Save and test

    Save the file, then test the site.

    If something goes wrong, restore the previous version of the file using SFTP, FTP, your hosting file manager, or your backup.

    Learn how to create a Divi child theme

    If you don’t already have a child theme set up, Elegant Themes has a guide to creating a Divi child theme.

    A child theme is a good option if you’re comfortable working with theme files and want a more developer-managed place for custom PHP, CSS and template-level changes.

    Conclusion

    There are several ways to add PHP code to Divi, but the safest option depends on where the code needs to run.

    Use Divi PHP Code Module when the PHP output needs to appear inside a specific Divi page, post, Theme Builder template or layout. Use Code Snippets or a Divi child theme when the PHP should run across the site.

    Avoid editing Divi’s parent theme files directly, as changes made there may be lost when Divi updates.

    Not sure which method your snippet needs? Leave a comment below with what the code is meant to do, and I’ll try to point you in the right direction.

    FAQs

    Can Divi’s Code Module run PHP?

    No. Divi’s built-in Code Module does not execute PHP code. It is intended for code such as HTML, CSS, JavaScript, shortcodes and embeds. If you paste PHP into the standard Divi Code Module, it will not run as server-side PHP. Use Divi PHP Code Module for layout-level PHP instead.

    How do I add PHP code in Divi Builder?

    Use Divi PHP Code Module. Once installed, add the PHP Code module to your layout, enter your PHP code in the module settings, and the output will display where the module is placed.

    Why is my PHP code showing as text instead of running?

    PHP code will show as text if it is added somewhere that does not execute PHP, such as Divi’s standard Code Module, Text Module, or another HTML output area.

    Should I add PHP to Divi’s functions.php file?

    Avoid editing Divi’s parent theme functions.php file directly. Use a child theme’s functions.php file or a code snippets plugin instead.

    Should I use Divi PHP Code Module or Code Snippets?

    Use Divi PHP Code Module when the PHP output needs to appear inside a Divi layout. Use Code Snippets when the PHP should run sitewide or modify WordPress / Divi behaviour more generally.

    Can I add PHP to a Divi Theme Builder template?

    Yes. Use Divi PHP Code Module if you want PHP output to appear inside a Theme Builder layout.

    Where should I put PHP code copied from a Divi tutorial?

    If the code outputs content in a specific layout location, use Divi PHP Code Module. If it uses hooks, filters, shortcodes or changes sitewide behaviour, use Code Snippets or a child theme.

    Run PHP Code Directly in your Divi Layouts with Ease

    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. Perfect for integrating custom PHP solutions into your Divi-built site without altering core files or risking theme updates.

    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, 

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

    37 Comments

    1. Hi Dan,

      I use the plug-in "code snippets" to insert PHP-coding like this. Works fine.

      Best regards
      Stefan

      Reply
      • Thanks Stefan, yeah Code Snippets seems to be a good option for adding / managing PHP on the site. I've used it a couple of times in the past and it has always worked well :)

        Reply
    2. Thanks a lot, this was very helpfull

      Reply
      • You're welcome, Samuel!

        Reply
    3. I need to add a code (automatic plug-in updates) to the functions.php file. I am not a website developer, and don't know how to get to the page in question. Can you please give me the steps? I understand it's the /wp-content/themes/Divi/functions.php, but where is it?
      Thank you

      Reply
      • Hi Mimi, if your web host has a file browser / file manager feature, you can find the file by opening the folder containing your website files (often called "www" or "htdocs"). There you should see a "wp-content" folder and within that a "themes" folder which itself contains a folder called "Divi". Open that and the functions.php file will be in there. You can do a similar thing using an FTP program to directly access the website files, rather than going through the file browser / file manager.

        That said, you should be aware that any updates to Divi will erase your code. You might want to consider using a child theme, since it will give you a functions.php you can add your code to, but which won't be affected by Divi updates.

        I hope that helps!

        Reply
    4. I have replaced the footer with a Divi Section, Global.

      Reply
    5. I have the latest Divi Booster (3.0.9) but I can't get the [year] to work. What does "…or install Divi Booster which will add the code for you,…" actually mean? I don't see it listed any place. Where is it? What do I do?

      Reply
      • Hi Randy, hopefully the global section (as per your follow up comment) is doing what you need, but if you still want to get the year shortcode to work, are you able to set me up an example so that I can take a look?

        The "…or install Divi Booster which will add the code for you,…" I posted in reply to Steven's comment related to Divi Booster's feature for enabling the Divi Builder on custom post types. What I meant was that, if the Divi Booster feature for enabling the Divi Builder on custom post types was activated, Divi Booster would add the PHP code to the site necessary to achieve that (as opposed to manually adding the equivalent code from within the linked post). As Divi now includes the in-built ability to enable the Divi Builder on custom post types, the Divi Booster feature / code in question is no longer necessary. As such, you should just be able to ignore my reply to that comment entirely.

        Let me know if there's anything else I can help with. Thanks!

        Reply
    6. Thank you so much!!! you saved me

      Reply
      • Glad I could help, Jeffo! :)

        Reply
    7. I am having this problem “Your PHP installation appears to be missing the MySQL extension which is required by WordPress” when I’m upgrade the php from version 5.6 to 7.2. Turns out that mysql extension is no longer supported in php version 7.2. It is now using mysqli extension. I am using old version of wordpress which still using mysql extension so the problem existed. So what I did is upgraded the wordpress to the core. I used the latest version of wordpress and used filezilla to put the new version to the site. I only overwrite the folder wp-includes and wp-admin and replace all the files in the root folder with the new files from version 7.2

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

    News