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

      Any tips about adding the php file to enable we courseware to be configured with the divi builder (or any other tips about using WP courseware with Divi to get the best out of it?😜)

      Thanks

      Reply
      • Hi Steven, I haven't tested it specifically with WP Courseware, but this general method of enabling the Divi Builder on custom post types (such as those used by WP Courseware) should work:

        https://divibooster.com/enable-divi-builder-on-custom-post-types/

        If you add the code manually (such as in a child theme's functions.php file), or install Divi Booster which will add the code for you, then that should enable the Divi Builder on WP Courseware. If it doesn't help, or you spot any conflicts with WP Courseware, let me know and I'll try to get it working for you. Thanks!

        Reply
    2. Hello, I'm Brazilian and I need help. I added the PHP code to the Divi theme file functions.php directly and now my site went offline. What do I do? Please help me.

      Reply
      • Hi Lucas, sorry to hear this. This can happen if there is a mistake in the PHP or if it is added incorrectly. One way to fix it is to delete the code you added (being careful to delete exactly the code you added and nothing else) and resave the functions.php. Another is to replace the functions.php with the original copy of functions.php from the same version of Divi that you're using. So if you are on the latest version of Divi you could re-download Divi and copy the functions.php file out of it. If you are on an older copy of Divi then you might not be able to get hold of the correct functions.php version. In that case I'd recommend downloading the latest version of Divi and uploading it (by FTP) to the themes directory of Wordpress, overwriting the existing directory (take a backup of the existing directory first). Let me know if none of these fix it for you.

        Reply
    3. I'm working with a DIVI support advisor and he is telling me I have to update my php to 5.7 and to let my hosting company know. It's currently running 5.52. Is this going to be the answer for some issues I have? I doubt the hosting company is in a position to simply upgrade my php….is that something they can do easily and will do?

      Reply
      • Hi Peter, it's certainly possible that updating php would solve some issues. A plugin, for example, that has been programmed using the newer features of php might cause errors when run on an older version of php. Whether php will solve your specific issues I can't say without knowing the details – did your advisor say why he thinks updating to php is necessary?

        Your hosting company may in fact be in a position to simply upgrade your php. Mine for instance (just a regular hosting plan) offers a range of php versions for my account and even gives me the ability to change it myself from my account control panel.

        It's important to be aware that updating php can itself introduce other issues (e.g. if you're running an older plugin which relies on an older version of php). To minimise any issues, you may want to backup your site first, apply the latest plugin updates and perhaps temporarily disable any not-so-important plugins. Also, do check if your host gives you the ability to change the php version yourself as this would let you try out the new version and then easily revert back to the old version if need be (your host could revert for you – it might just take a bit longer).

        Hopefully that helps, but if you like feel free to give me some details of the issues you've been having and I'll try to advise you on (what I think is) the best course of action.

        Reply
    4. Hi Dan,

      What to do if you want to REPLACE some code in the functions.php using a child theme?

      Reply
      • Hey Jonas, some of the functions in Divi's functions.php are "pluggable". These functions start with a check like:

        if ( ! function_exists( 'et_divi_fonts_url' ) )
        

        This means your child theme's functions.php can define these functions and your definitions will be used instead of Divi's. Start by copying the entire function into your functions.php, then make the changes you want there.

        Note that this is not ideal, as any improvements / changes to the function made in future versions of Divi won't be reflected in your site.

        Is there something in particular you are trying to do? Sometimes it will be possible to come up with a workaround (using hooks and filters, etc) that ends up having the same effect as directly replacing the code.

        Reply
    5. Hi I want to modify the way divi shows when you access an authors page. Now it only appears the lastest posts of the author. I just want to add the photo and description. I saw some papers about creating an author_page.php but only related to other themes. Maybe you know some tutorial or something to make this possible in Divi? Thank you

      Reply
      • Hi Galo, I couldn't find any Divi-specific tutorials for this, so I've started my own. It's a bit rough, but hopefully it gives you a bit of a start:

        https://divibooster.com/customizing-divi-author-pages/

        As detailed in the post, it's possible to a variant of the author_page.php method you mention. There's also a plugin that lets you use the Divi Builder on the author pages.

        Reply
    6. Hi – I'm trying to add a subtitle to the paget title that will show on category archive pages. I've added php to my functions.php as shown here and it's inserting almost exactly like I want it to. My problem is that I want to insert before and after my insert and it's showing in title like this: My Subtitle Text so I can read the html in the title. Any idea why?

      Reply
      • Code examples were removed in the about question when I posted. For clarity, I just want to have span tags to add a class to my text insert. The html is showing in the subtitle as if it were text and not code. Not sure how to address this.

        Reply
        • Hi dpeschio, I think I get what you're saying. It sounds like the span tags are being "html entity encoded" which just means they are being converted from HTML into plain text that will display as is on the page. The solution is to avoid the html entity encoding of the span tags, but how you do this will depend on what you have in the functions.php file. I've just sent you an email. If you'd like to reply to that with a copy of the code you're adding, I should be able to tell you what needs to be done. Cheers.

          Reply
    7. I want to add a snippet to the DIVI Theme functions.php but it has no closing tags like this ?>

      So I don't know if I can paste the snippet just at the end of th page .. weird.

      Reply
      • Hi petra, it might be helpful to understand what exactly the php tags do. Basically, they switch the language used in the .php file between HTML and PHP. Starting from the top of the file, any code is intially treated as HTML. However, when an opening <?php tag is encountered, the file switches into PHP mode and subsequent code is treated as php code. Then, if a closing tag ?> is encountered, the file switches back to HTML mode. This can happen repeatedly in a .php file.

        So when adding code, you need to be aware of what mode the file is in at the point you paste your code. If it is in HTML mode, and you want to insert PHP code, you need to surround the PHP code in opening and closing tags, so that it switches to PHP mode for your code and then returns to HTML mode once done.

        In most cases, things are quite simple in the functions.php file. As this file isn't usually involved in outputting HTML, it is usually in PHP mode from start to finish. As a result, you can usually paste in PHP code without either the opening or closing tag. So probably you'll be able to paste your code at the end of the page (before any existing final closing tag), without including either the opening / closing tags.

        If you're still uncertain, as long as you are able to reverse your changes (e.g. by keeping saving a copy of the functions.php file before you start), you can probably just try putting in your code and seeing what happens. If you get something wrong, you'll almost certainly know about it as the site will most likely break. Hence why you need to be able to reverse your changes!

        Reply
    8. Hello, I do not see why the full code in php.footer

      Reply
      • Hi Paul, I'm not sure I understand what you're asking, sorry. Is there any chance you can explain in a bit more detail what you're asking? Feel free to write in another language if this is easier – I'll translate. Cheers!

        Reply
    9. I want to add a small chunk of code to a page. Something like:

      display() ;
      ?>

      Is that possible?

      Reply
      • I guess it didn't like what I put:

        include( "displayClass.php" );
        $addressBook = new displayData( "addressBook" ) ;
        $addressBook->display() ;

        Reply
        • Hi Jeff. In theory, yes, you should be able to add this code. However there are a couple of things to be aware of. First is that it tries to load the displayClass.php file from the same directory. That means wherever you use the code, you'll probably want to have the displayClass.php file in the same folder. For example, if you were to put the code into one of the .php files in the root folder your child theme, you probably would need to put a copy of displayClass.php into the child theme root folder as well. Second, I'm guessing that the code outputs html to display the address book, and that it does so at the time it is run. This means that the place where you put this code is important – as that's where the address book will show up. This means, for example, that you couldn't just add it into the functions.php file (or it would be displayed before anything else in the page). Depending on what you need, you may find it useful to use a plugin such as this:

          https://wordpress.org/plugins/insert-php/

          It lets you add PHP anywhere you'd normally be able to use a shortcode. Note that if you do, you may have problems with the location of the displayClass.php file. Probably the way to solve that would be to change the include() line to specify the full file path for the displayClass.php file, rather than just the file name.

          Hopefully that helps a bit!

          Reply
      • Hi Richard, if you're using a child theme, it will usually be in the main directory of the child theme and called functions.php. Otherwise, Divi's own functions.php file is usually found at /wp-content/themes/Divi/functions.php within your WordPress installation directory.

        Reply
    10. Can i create own module inside divi builder??

      Reply
      • It's technically possible, but not at all easy. The way to do it would be to copy the code for one of the existing Divi modules (located in /includes/builder/main-modules.php) and modify it to suit your needs.

        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