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.

A Quick Warning

First, 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).

Where to Add PHP Code to Divi

  • Add PHP code to the Divi theme functions.php file – The traditional way to add PHP code to a theme has been 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). The major downside with this method is that if you update the theme the functions.php file will be overwritten and your changes lost.
  • Add PHP code to a Divi child theme – To solve the above problem, 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 via a plugin – The free 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.

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.

This post may contain referral links which may earn a commission for this site

Divi Booster

Hundreds of new features for Divi
in one easy-to-use plugin


  1. Hi Dan,

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

    Best regards

    • 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 :)

  2. Thanks a lot, this was very helpfull

    • You're welcome, Samuel!

  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

    • 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!

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

  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?

    • 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!

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

    • Glad I could help, Jeffo! :)

  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


Submit a Comment

Your email address will not be published. Required fields are marked *