Modify Divi With PHP Snippets
There are many situations that require you to modify the core structure of your Divi website or add some additional functionality. Many tutorials on our blog and other WordPress functions require pasting PHP code into your website, but unfortunately there is no option in Divi to paste the snippet or to extend the theme with PHP code. In this tutorial, I will show you where to add custom PHP code snippets in Divi that you copy from our Divi many tutorials or other sources.
▶️ Please watch the video above to get all the exciting details! 👆
Places To Add PHP Code In Divi
In this guide, we will explore several places you can add PHP snippets in your Divi-based WordPress website. There is no right or wrong here, and no better or worse – this all depends on what is the most convenient for you and what fits best into your workflow.
A Child Theme’s Functions.php File
As a Divi user, you may have heard about something called a child theme. A child theme is an optional addition to the parent Divi theme that opens up lots of customization opportunities. We won’t go into what a child theme is here, but you can learn more about it in our guide on How To Generate A Divi Child Theme Directly On Your Site.
Any custom PHP snippets should be placed in the Divi child theme’s functions.php file, which is the main file of any child theme. If you are copying a PHP snippet from one of our many tutorials, or from another site, you can paste it here.
- Navigate in your WordPress admin area to Appearance>Theme File Editor
- Select your child theme from the dropdown in the top right corner.
- Click on the functions.php tab on the right
- Paste your code into the code editor area
A Dedicated Code Snippet Plugin
If you are not using a child theme, or if you prefer to have each of your PHP snippets labeled and activated separately, you can check out any of the “code snippet” related plugins in the WordPress plugin repository. A new one that just released is very clever and secure called “FluentSnippets” and it has some great features, like the ability to label and activate or deactivate each snippet, conditional logic, error handling, and more. There are many others to choose from too, so it all depends on your preferences.
Any code snippet plugin will work similarly. They often have a code editor area to paste snippets for CSS, JS, and PHP. However, in the Divi theme we already have dedicated places to add code like that.
Using The Divi Assistant Plugin
If you are using our Divi Assistant plugin, then you can literally add custom PHP code snippets in the Divi Visual Builder in our custom code window! That’s right, we have invented a convenient new way to add PHP code in Divi by adding a new custom code editor window in the Divi Visual Builder with a dedicate PHP editor!
Custom PHP code must be handled carefully when working on live websites. Incorrectly formatted or written PHP code can cause errors to display, or even worse, break the site completely by causing a fatal error. However, if there is a fatal error caused by custom PHP code that you add to the PHP tab of the Divi Assistant code editor, you don’t need to worry! This is because we have created several special error handling features for this situation.
- The PHP code will not run unless this setting is enabled. This means if there is any issue, you can always come here to simply disable this setting, and the PHP code will no longer run. This provides the opportunity to fix the error in the code without displaying an error message or breaking the site.
- The PHP code will not run when the Visual Builder is open. This is intentionally designed to allow you to access the code editor even if there is an issue.
- If there is any fatal error caused by PHP code in the code editor provided by Divi Assistant, it will show the error details at the bottom of the website on the frontend instead of crashing the site. You can then come here to disable the code or enable the Visual Builder, open the code editor, and edit or remove the code to fix the error.
Enabling the additional setting in Divi Assistant will activate and run the PHP code in the PHP tab in that custom code window in the Divi Visual Builder.
Where To Add PHP Code Snippets Using Divi Assistant
Here are the simple steps to add PHP code snippets to Divi using our popular Divi Assistant plugin:
- Install and activate the Divi Assistant plugin
- Click on the Code Helper tab
- Enable the PHP tab settings
I hope that is easy enough for you! 😉
will divi assistant play nicely with divi pixel?
Sure, I don’t see why not.