Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active November 26, 2016 06:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishterk/9b6801567286f62538d5ec085eed255b to your computer and use it in GitHub Desktop.
Save mishterk/9b6801567286f62538d5ec085eed255b to your computer and use it in GitHub Desktop.
Divi hacks, mods, fixes, etc
<?php
add_action( 'et_builder_ready', function () {
class MyCustomModule extends ET_Builder_Module {
function init()
{
$this->name = esc_html__( 'My Custom Module', 'textdomain' );
$this->slug = 'et_pb_my_custom_module';
$this->fullwidth = false;
$this->whitelisted_fields = array(
'image_url',
'overlay_text',
'link_url',
);
}
function get_fields()
{
return array(
'image_url' => array(
'label' => esc_html__( 'Background Image URL', 'textdomain' ),
'type' => 'upload',
'option_category' => 'basic_option',
'upload_button_text' => esc_attr__( 'Upload an image', 'textdomain' ),
'choose_text' => esc_attr__( 'Choose an Image', 'textdomain' ),
'update_text' => esc_attr__( 'Set As Image', 'textdomain' ),
'description' => esc_html__( 'Upload your desired image, or type in the URL to the image you would like to display.', 'textdomain' ),
),
'overlay_text' => array(
'label' => esc_html__( 'Text', 'textdomain' ),
'type' => 'text',
'description' => esc_html__( 'Set the overlay text', 'textdomain' ),
),
'link_url' => array(
'label' => esc_html__( 'Link URL', 'textdomain' ),
'type' => 'text',
'description' => esc_html__( 'Input the destination URL.', 'textdomain' ),
),
);
}
function shortcode_callback( $atts, $content = null, $function_name )
{
$overlay_text = $this->shortcode_atts['overlay_text'];
$image_url = $this->shortcode_atts['image_url'];
$link_url = $this->shortcode_atts['link_url'];
// TODO - return some markup here...
return 'My groovy markup';
}
}
new MyCustomModule;
} );
<?php
// Hiding the Divi builder toggle everywhere except pages, until we are ready
add_action( 'admin_head', function () {
?>
<style>
.et_pb_toggle_builder_wrapper {
display: none;
}
.post-type-post .et_pb_toggle_builder_wrapper.pds-visible,
.post-type-page .et_pb_toggle_builder_wrapper.pds-visible {
display: block;
}
</style>
<script>
(function ($, window, document, undefined) {
$(document).ready(function () {
var $templateSelect = $('#page_template, #post_template');
evaluate();
$templateSelect.on('change', evaluate);
function evaluate() {
var val = $templateSelect.val();
if (
val === 'templates-page/blank-canvas-for-divi.php'
|| val === 'templates-page/standard-builder-for-divi.php'
|| val === 'single-blank-canvas.php'
) {
$('.et_pb_toggle_builder_wrapper').addClass('pds-visible');
}
else {
$('.et_pb_toggle_builder_wrapper').removeClass('pds-visible');
}
}
});
})(jQuery, window, document);
</script>
<?php
} );
<?php
/**
* Adding a link to the WP admin bar user dropdown menu that enables users to reset all custom Divi modules. Clicking
* this is necessary after pushing updates to an existing module, as Divi stores module admin markup in local storage.
*
* If a user cannot see the 'Reset custom Divi module local cache' link, then local storage is not supported in their browser.
*/
add_action( 'admin_footer', 'ctips_divi_module_refresh_js' );
add_action( 'admin_footer', 'ctips_divi_module_refresh_js' );
function ctips_divi_module_refresh_js()
{
?>
<script>
(function ($, window, document, undefined) {
$(document).ready(function () {
if (typeof(Storage) === "undefined") {
console.log('Local storage is not supported in this browser – the \'Reset custom divi module local cache\' link is not available in the user dropdown menu.');
return;
}
var $adminBarMenu = $('#wp-admin-bar-user-actions');
var $newItem = $('<li><a class="ab-item" href="#">Reset custom Divi module local cache</a></li>');
$newItem.appendTo($adminBarMenu);
$newItem.find('a').on('click', function (e) {
e.preventDefault();
clearModules();
});
});
function clearModules() {
for (let key in localStorage) {
if (localStorage.hasOwnProperty(key)) {
if (key.indexOf('et_pb_templates_et_pb_ctips') == 0) {
localStorage.removeItem(key);
}
}
}
var prompt = confirm('Custom Divi modules have been removed from local storage. You\'ll need to reload the page see any updates to these modules. Click OK to reload the page.');
if (prompt === true)
location.reload();
}
})(jQuery, window, document);
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment