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.
There are other ways to add PHP to the Divi theme, which I hope to expand on in the future, but for now these are the two main options.
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.