Using the Code Snippets plugin with Divi

Written by Dan Mossop

As a Divi developer, you may sometimes need to add custom code to your Divi theme. While it's possible to add it via a child theme, a custom plugin, or (in some cases) Divi's various options, these may not always be the most convenient method. To add custom code to the Divi Theme and manage it in a single location, you can use the free Code Snippets plugin for PHP code, or the premium Code Snippets Pro plugin for JavaScript, CSS, and HTML code. Both plugins offer an easy way to organize your code and keep things in order.

Note: Code Snippets is not to be confused with my own Divi Code Snippets module (which is designed for displaying, not running, code).

Adding PHP Code to the Divi Theme using Code Snippets

Divi itself doesn't include a way to add PHP code to your site. While it's possible to add PHP code using a child theme or custom plugin, these methods can be difficult for non-developers to set up or manage. Similarly, they may be overkill if you just want to try out a quick code snippet to your site.

This is where the Code Snippets plugin comes in handy. It offers an easy and organized way to add custom PHP code to your Divi theme. Code snippets can automatically load on the front-end or can be added to specific pages, posts or categories. You can also activate them / deactivate them at the click of a button, useful for time-limited code such as that relating to seasonal promotions.

Here's how you can use Code Snippets to add PHP code to your Divi Theme:

  1. Install the Code Snippets plugin from the Wordpress Plugin Repository.
  2. Activate the plugin, and then navigate to Snippets > Add New in your WordPress dashboard.
  3. Give the snippet a title and then add the desired PHP code.
  4. Select "Only run on site front-end" if you want the PHP code to run only on the front-end of the site, or "Run snippet everywhere" if you want it to run on both the front and back-end.
  5. Save and activate the snippet.

That's it. You've now added custom PHP code to your Divi site using Code Snippets.

Adding JavaScript / CSS to the Divi Theme using Code Snippets Pro

Divi includes a range of places where you can add CSS and JavaScript code, for example:

  • The "Divi > Theme Options > General > Custom CSS" option
  • The "Divi > Theme Options > Integration" options
  • The built-in Code Module
  • The Divi Code Snippets feature
  • The Custom CSS option in individual Page / Post settings
  • The Custom CSS options in the Advanced Tab of modules

For many, some combination of these options will suffice. However, as the number of code snippets added increases, it can be come hard to keep track of and manage them. If you'd rather have (or give your clients) a central point for managing CSS and JavaScript code snippets (in addition to PHP code), then Code Snippets Pro may be the ideal solution.

Code Snippets Pro, the premium version of the standard Code Snippets plugin, adds the ability to add and organize JavaScript and CSS in addition to PHP code. As with the free version, you can add your code snippets site-wide or on specific pages and posts, and can categorize your code snippets to make them easier to find and manage.

Here's how to use Code Snippets Pro:

  1. Install and activate Code Snippets Pro plugin.
  2. Navigate to Code Snippets > Add New in your WordPress dashboard.
  3. Give the code snippet a title.
  4. Choose the type of code you want to add: Scripts (JS) or Styles (CSS)
  5. Add the desired code. You can also use the code editor to write your code.
  6. Select "Only run on site front-end" if you want the PHP code to run only on the front-end of the site, or "Run snippet everywhere" if you want it to run on both the front and back-end.
  7. Save and activate the snippet.

If you're interested in using Code Snippets Pro, use the discount code "divibooster" to get 10% off your purchase.

 

Effortlessly Display Code with Divi Code Snippet Module

The Divi Code Snippet Module allows you to beautifully present syntax-highlighted code within your Divi pages, perfect for showcasing your HTML, CSS, PHP, or JavaScript code in a way that's easy to read and understand. Whether you're a developer or a designer, it's a must-have for clean and effective code display.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

0 Comments

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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

We may earn a commission when you visit links on our website.

Latest Posts

Set Custom CSS IDs for Individual Divi Accordion Items

Assigning unique CSS IDs to specific Divi Accordion items allows for precise control over styling, targeting, and linking within your page content. This ability is particularly useful when you want to apply custom designs or create anchor links to particular accordion...

Enable Swipe Navigation in the Divi Gallery Lightbox

Enabling swipe navigation in the Divi Gallery module's lightbox allows users to seamlessly browse through gallery images by swiping left or right, creating a more interactive and mobile-friendly experience. This functionality can significantly improve user engagement...

Disable Slide-In Animation for Divi Gallery Grid Images

Control how images appear in your Divi Gallery module by toggling the slide-in animation effect for grid layouts. Disabling the slide-in animation allows gallery images to load instantly and appear statically, providing a faster and distraction-free browsing...

Control Image Count Display in Divi Gallery Lightbox

Displaying or hiding the image count in the Divi Gallery module’s lightbox can help customize the user experience, depending on whether you want to give visitors an indication of gallery progress or prefer a cleaner, distraction-free view. The ability to toggle this...

Hide Gallery Image Titles in the Divi Lightbox Overlay

Displaying image titles in the lightbox overlay of the Divi Gallery module can sometimes be distracting or unnecessary, depending on your website’s design and user experience goals. Hiding these titles creates a cleaner and more focused viewing experience for visitors...

Random Posts