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