Adding PHP code to the Divi Theme

Some of my fixes for the Divi theme require PHP code to be added to make the required change to the theme. This post briefly covers the various options and their various advantages.

To add PHP code to the Divi Theme, you can:

  • Use Divi PHP Code Module – to add PHP directly in Divi pages
  • Use a child theme – to extend Divi's functions.php file
  • Edit Divi's functions.php file – not recommended!
  • Use a code snippet plugin – to add PHP globally

Add PHP code to the Divi with Divi 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.

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.

The main benefit of Divi PHP Code module is its integration with Divi Builder, meaning you can conveniently add the PHP code right into your Divi builder layouts where you actually want to use it, and treat your blocks of code as modules.

Unlike the other methods discussed below, the PHP code is only run in the posts / pages / layouts in which you add the Divi PHP Code module – if you want to add PHP code that runs everywhere on the site, the other methods below may be more suitable.

Divi PHP Code Module

Unleash the power of PHP within your Divi Builder using the innovative Divi PHP Code Module. Seamlessly add and execute PHP code directly in your Divi pages, opening up limitless possibilities for dynamic functionality. 

(DON'T) Add PHP code to the Divi functions.php file

One way to add PHP code to a theme is to add it directly into the theme's functions.php file. For Divi this is typically located at /wp-content/themes/Divi/functions.php. Where you place the code doesn't usually matter, so you can just place it at the end (but see the section below on PHP tags). But I'd highly recommend you don't add PHP code in this way as there is a major downside with this method. Whenever Divi is updated the functions.php file will be overwritten and your changes lost.

Add PHP code to Divi with a Child Theme

To solve the problem of the theme's functions.php file being overwritten on updates, child themes were invented. A child theme loads the main theme (Divi) and enhances it with the contents of its own files. For instance, PHP code in the child theme's functions.php file will be automatically "added" to the main Divi theme's functions.php file. The great thing about this is that code placed in the child theme is not overwritten when the main theme is overwritten.

Add PHP code to Divi using the Code Snippets plugin

The Code Snippets plugin lets you add and organize PHP code to be run on the site, and be a convenient way to do so without needing to modify theme files or use a child theme.

A Quick Warning

A word of caution. PHP code added to Divi effectively runs as part of WordPress itself (which is also written in PHP). This means that errors in the PHP code or how it is added can break your site and prevent it from loading. Be sure to take a backup of any files you intend to modify, so that you can restore the old copy if anything goes wrong. Also make sure you have a way of restoring the files which doesn't rely on the WordPress dashboard and editor which may not be accessible if something goes wrong (use FTP, SFTP or your web host control panel instead).

Understanding PHP Tags

While you don't need to know PHP code to copy in code from the tips I post, you do need to understand PHP tags to make sure you are placing the code in the right place. Placing the code incorrectly could cause errors (see the warning above) or simply cause the code not to work.

A PHP file has two modes: PHP mode and HTML mode. By default anything placed in a PHP file will be processed in HTML mode meaning, more or less, that it will be output straight to the browser without being executed. However, many files (such as functions.php) need to actually have code executed to do something useful. To achieve this, they switch into PHP mode by including an opening PHP tag (often at the very start of the file).

Once in PHP mode, anything placed in the file will be treated as PHP code and executed. It is possible to switch back to HTML mode at any time by including the closing tag, but for a file like functions.php this generally won't happen until right at the end of the file (and sometimes there will be no switch back to HTML).

When adding PHP code to a functions.php file, the main thing to ensure is that the code is added in a place which is operating in PHP mode (so somewhere between an opening and a closing tag). The best place is usually just before the final closing tag, or right at the end of the file if there is no final closing tag.

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

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