Using the Divi Code Snippet Module from a Shortcode

Written by Dan Mossop

My Divi Code Snippet Module lets you easily display source code snippets in your Divi Builder layouts. However, there may be times you want to add a code snippet to a page when you don't have the Divi Builder enabled on that page. It can be done by using the module's shortcode.

Like all Divi modules, the code snippet module is represented internally in your posts by a shortcode. Here's an example of such a shortcode:

[et_pb_dmb_code_snippet admin_label="My Title" _builder_version="3.0.82" title="My Title" style="default" linenums="off" usetabwidth="off" border_style="solid" box_shadow_position="outer"]PD9waHAgZWNobyAiaGVsbG8gd29ybGQhIjsgPz4=[/et_pb_dmb_code_snippet]
You'll notice in the shortcode above that there is a "garbled" bit of text (PD9waHAgZWNobyAiaGVsbG8gd29ybGQhIjsgPz4=). This is actually the source code the module will display, but it is base64 encoded to stop it from being altered by either Divi or WordPress. If you enter this into an online base64 decoder and hit decode you'll get back the original source code, which in this case is:
<?php echo "hello world!"; ?>
Add your own source code to the shortcode, you can paste your source code into a base64 encoder. This will convert your source code into a garbled base64 string, which you can then us in the shortcode given above.

Another way to generate the shortcodes is to create a page in the Divi Builder, add your code snippet module, save the page as a draft and then temporarily disable Divi / the Divi Builder (e.g. by switching themes, or deactivating the Divi Builder plugin. If you then reload the page edit screen and switch to the "Text" tab of the default editor, you'll see the shortcode. Obviously, since this method requires disabling the theme, it is best to do it on a test site rather than a live site.

Creating an Alias for the Shortcode

If you'd like to simplify things, you might consider creating a new shortcode that generates the code snippet module shortcode, preconfigured with your choice of design, and which takes care of the base64 encoding for you so that you can enter code directly. Here is an example of the PHP code to create such a shortcode:

// Create alias for code snippet module shortcode
add_shortcode('codesnippet', 'db_codesnippet_shortcode_wrapper');

function db_codesnippet_shortcode_wrapper($atts, $content="") {
	return do_shortcode('[et_pb_dmb_code_snippet admin_label="My Title" _builder_version="3.0.82" title="My Title" style="default" linenums="off" usetabwidth="off" border_style="solid" box_shadow_position="outer"]'.base64_encode(html_entity_decode($content)).'[/et_pb_dmb_code_snippet]');
}
Now you can use the new shortcode in your posts like so:
[codesnippet]<?php echo "hello world"; ?>[/codesnippet]
If using this aliasing technique, I suggest checking your code snippets for correctness once generated. By delaying the base64 encoding, there is the possibility that some code samples may not display correctly.

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

Showcase Your Code Beautifully with Divi

Effortlessly display syntax-highlighted source code in your Divi Builder layouts with the Divi Code Snippet Module. This tool supports over 70 languages and makes your code presentations visually appealing and easy to understand.

Latest Posts

Customize Lightbox Title Styling in the Divi Gallery Module

Adjust the appearance of the lightbox title that appears when visitors open images from a Divi Gallery to align with your brand and improve readability. Refining the title’s color, size, and weight enhances visual hierarchy, accessibility, and overall polish. In this...

Center the Lightbox Image Count in the Divi Gallery Module

Centering the image count in a gallery lightbox creates a cleaner, more balanced presentation and makes it easier for visitors to see where they are in the image set. This simple visual tweak can enhance readability, especially with larger images and longer captions,...

Style the Divi Gallery Module Lightbox Image Count

Tailor the appearance of the image counter in the Divi Gallery module lightbox to match your brand and improve readability. By customizing the counter’s color and typography, you ensure it displays clearly and suits your design. In this guide we show you how to style...

Customize the Lightbox Image Background Color in the Divi Gallery

Control the color that appears behind your images when users open in the gallery lightbox, including support for semi-transparent hues. This helps improve contrast for transparent images, align the experience with your brand palette, and create a more polished viewing...

Style the Divi Gallery Lightbox Close Button

Give your gallery lightbox a clear, on-brand close button by customizing its color and size. This improves visibility and usability against varying image backgrounds and ensures the button renders at full opacity for a consistent experience. In this guide we show you...

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, 

2 Comments

  1. Hi there,

    I found that this example crashes WordPress with a not found for DMB_Module_Code_Snippet.

    so, I added this within the init function (copied from your own plugin):

    if ( !class_exists( 'db_cs_ET_Builder_Module' ) || class_exists( 'DMB_Module_Code_Snippet' ) ) {
    return;
    }

    and that stops the crash.

    WordPress: 5.5.3
    Divi: 4.6.6

    It does seem to work now, so I don't follow why it would crash unless there is a loading sequence issue.

    I used this for embedding a ACF custom field with the code which I can't do via the module e.g.

    [codesnippet] [field shortcode_code][/codesnippet]

    Cheers,

    Reply
    • Hi David, thanks for letting me know about this. The crux of the issue is that the code in the post was getting a bit old and didn't account for recent changes to the plugin. The plugin now defines the class later, after that init function runs, so it was indeed a loading sequence issue. Your chance will have effectively disabled the init function, which is fine since it is no longer necessary – the plugin loads the resources it needs more intelligently. I have updated the code in the post to remove the init function entirely, which should resolve the error. Thanks again!

      Reply

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