You can add PHP code in Divi Builder by creating shortcodes, using plugins like Code Snippets, or editing the functions.php file of a child theme.
This guide explains these methods step-by-step to help you safely and effectively extend Divi’s capabilities with PHP.
Methods to Add PHP Code in Divi Builder
1.1 Using the Code Snippets Plugin (Recommended for Beginners)
Why it works: The plugin allows you to add and manage PHP code snippets without touching theme files.
Steps:
- Install and activate the Code Snippets plugin from the WordPress repository.
- Navigate to Snippets > Add New in the WordPress admin panel.
- Enter a descriptive title for your snippet and paste the PHP code in the provided field.
- Choose where the snippet will execute (sitewide, specific pages, etc.).
- Save and activate your snippet.
Advantages:
- Easy to use with no need to modify theme files.
- Keeps code organized and manageable.
1.2 Converting PHP into Shortcodes
Why it works: Shortcodes let you execute PHP code within Divi Builder’s Text Module.
Steps:
- Open the functions.php file in your child theme (explained below).
- Add the following code to define a shortcode:
function my_custom_shortcode() {
// Your PHP logic here
return 'Dynamic output from PHP!';
}
add_shortcode('my_shortcode', 'my_custom_shortcode');
3. Save the file and use [my_shortcode] in any Text Module in Divi Builder.
Example Use Case: Display user-specific greetings or fetch data dynamically.
1.3 Editing the functions.php File of a Child Theme
Why it works: Keeps custom PHP code safe from theme updates.
Steps:
- Create a child theme (if not already created) by adding a new folder in wp-content/themes/ and including style.css and functions.php files.
- Add your PHP code to the child theme’s functions.php file.
- Save changes and upload the modified file via FTP or the WordPress Theme Editor.
Pro Tip: Always use a child theme to avoid losing customizations when Divi updates.
1.4 Using FTP for Advanced Modifications
For those comfortable with manual methods, you can directly edit Divi’s files via FTP. Locate the functions.php file under wp-content/themes/Divi/ and add your code. However, this method is risky and not recommended unless you have experience with PHP and server-side operations.
Real-World Applications of PHP in Divi
Dynamic Content
Display personalized greetings:
function display_user_greeting() {
if (is_user_logged_in()) {
$user = wp_get_current_user();
echo 'Welcome, ' . esc_html($user->display_name) . '!';
} else {
echo 'Welcome, Guest!';
}
}
Integrating APIs
Use PHP to fetch and display data from an API:
function fetch_api_data() {
$response = wp_remote_get('https://api.example.com/data');
if (is_wp_error($response)) return 'Error fetching data';
return wp_remote_retrieve_body($response);
}
add_shortcode('api_data', 'fetch_api_data');
Automations
Automatically update custom settings or create reports using server-side logic.
Best Practices for Adding PHP in Divi Builder
- Backup First: Use plugins like UpdraftPlus to ensure you can restore your site.
- Test in Staging: Platforms like WP Stagecoach allow you to test changes safely.
- Validate Code: Use tools like PHP Code Checker to catch syntax errors.
- Use Commenting: Annotate code for easier troubleshooting.
Conclusion
Adding PHP code in Divi Builder unlocks a world of possibilities, from dynamic personalization to seamless integrations. For beginners, using the Code Snippets plugin is the easiest and safest method. Developers prefer shortcodes or child themes for more flexibility.