Settings Field Visibility

Settings Field Visibility

Settings Field Visibility
Learn how to show or hide a setting depending on the value of other settings.

Note: This tutorial series is intended for advanced users. At least a basic understanding of coding in PHP and JavaScript is required.
The visibility of a module setting can depend on the value of other settings by including one or both of the parameters described below in the setting definition.
Setting Visibility Parameters

show_if (array)

setting (string|string[]) — Setting value(s)

show_if_not (array)

setting (string|string[]) — Setting value(s)

Setting Visibility Examples
Only show setting_a when:

setting_b is on

array(

'label' => esc_html__( 'Setting A', 'myex-my-extension' ),

'type' => 'text',

'option_category' => 'basic_option',

'description' => esc_html__( 'Input something here.', 'myex-my-extension' ),

'toggle_slug' => 'main_content',

'show_if' => array(

'setting_b' => 'on',

),

),

'setting_b' => array(

...

),

);

}

...

view raw

module-setting-field-definition.php

hosted with by GitHub

 
Only show setting_c when:

setting_b is on
AND setting_a is not some_value

array(

...

),

'setting_b' => array(

...

),

'setting_c' => array(

'label' => esc_html__( 'Setting C', 'myex-my-extension' ),

'type' => 'text',

'option_category' => 'basic_option',

'description' => esc_html__( 'Input something else here.', 'myex-my-extension' ),

'toggle_slug' => 'main_content',

'show_if' => array(

'setting_b' => 'on',

),

'show_if_not' => array(

'setting_a' => 'some value',

),

),

);

}

...

view raw

module-settings-definition.php

hosted with by GitHub

 
Only show setting_a when:

setting_b is one of value_1, value_3, value_4
AND setting_c is not some_value
AND setting_d is not one of value_1, value_4

 

array(

'label' => esc_html__( 'Setting A', 'myex-my-extension' ),

'type' => 'text',

'option_category' => 'basic_option',

'description' => esc_html__( 'Input something here.', 'myex-my-extension' ),

'toggle_slug' => 'main_content',

'show_if' => array(

'setting_b' => array( 'value_1', 'value_3', 'value_4' ),

),

'show_if_not' => array(

'setting_c' => 'some_value',

'setting_d' => array( 'value_1', 'value_4' ),

),

),

'setting_b' => array(

...

),

'setting_c' => array(

...

),

'setting_d' => array(

...

),

);

}

...

view raw

pseudo-code.php

hosted with by GitHub

Advanced Field Types For Module Settings

Advanced Field Types For Module Settings

Advanced Field Types For Module Settings
Learn about the Advanced Field types for module settings.

Note: This tutorial series is intended for advanced users. At least a basic understanding of coding in PHP and JavaScript is required.
Advanced fields are added automatically to all modules that are compatible with the latest version of the Divi Builder unless they specifically opt-out. Most advanced fields are configurable. Configuration for advanced fields can be defined in the get_advanced_fields_config() method of the module』s PHP class. To opt-out of an advanced field, simply set its key in the array to false.
Background (background)
The background field allows users to set the module』s background color, gradient, image, and/or video.

Background Field Configuration

css (array) — CSS style configuration

important (bool) — Whether or not styles should include !important
main (string) — CSS selector for the module』s main element

options (array) — Field definition parameter overrides

module_setting_slug (array)

parameter_1 (mixed) — Parameter value
parameter_2 (mixed) — Parameter value

settings (array)

disable_toggle (bool) — Don』t add a Background toggle group to the settings modal
tab_slug (string) — Modal tab slug
toggle_slug (string) — Modal tab settings group toggle slug

use_background_color (bool|string) — Show the background color tab
use_background_color_gradient (bool|string) — Show the background gradient tab
use_background_image (bool|string) — Show the background image tab
use_background_video (bool|string) — Show the background video tab

The accepted values for use_background_color, use_background_color, use_background_gradient, use_background_image, and use_background_video are:

true — Display fields in the module settings and handle frontend markup
false — Do not display fields in the module settings and don』t handle frontend markup
fields_only — Display fields in the module settings but don』t handle frontend markup

Background Field Configuration Examples

Borders (borders)
The borders field allows users to set the module』s border style, color, width, & radius.

Border Field Configuration
Modules can apply borders not only to their outermost container element, but also to elements inside it. Each key in the configuration represents a separate element that will have adjustable border settings. The border field configuration for the module』s outermost container element is set under the default key.
While only parameters that are specific to borders fields are shown below, all of the common field parameters are supported.

default (array)

css (array) — CSS style configuration

main (array) — CSS selectors for the module』s main element

border_radii (string) — CSS selector to be used for border radius styles
border_styles (string) — CSS selector to be used for border styles

defaults (array)

border_radii (string) — Default value for border radius.
Format: link|top-left|top-right|bottom-right|bottom-left
Example: on|3px|3px|3px|3px
border_styles (array) — Default values for border style properties

width (string) — Default value for border width
color (string) — Default value for border color
style (string) — Default value for border style

label_prefix (string) — Text to display in front of the setting label (localized)
suffix (string) — Suffix to append to the setting slug. Not required for default border field

Borders Field Configuration Examples

Compatibility Levels

Compatibility Levels

Compatibility Levels
Making existing custom modules compatible with the latest version of the Divi Builder.

Note: This tutorial series is intended for advanced users. At least a basic understanding of coding in PHP and JavaScript is required.
How a custom module looks and behaves inside the builder depends on its level of compatibility with the builder. There are three levels of compatibility available to custom modules: off (this is the default), partial, and on. Custom modules can declare their level of compatibility using the vb_support property in their PHP class definition.
No Compatibility
This is the default setting for all custom modules. Live previews are not shown for modules with no support for the latest version of the Divi Builder. Instead, a generic block will be shown. The module』s settings can still be edited and it can still be moved around on the page.

Partial Compatibility
The builder will attempt to render a live preview via AJAX for custom modules that declare partial support. AJAX rendering is not ideal as it』s much slower than normal rendering and it won』t be suitable in all cases. For that reason, its important that developers test their modules and only declare partial support for modules that can actually be successfully rendered via AJAX.
Declaring Partial Compatibility Example

Full Compatibility
Modules that are fully compatible with the Divi Builder behave just like official modules. They use the builder』s JavaScript API to handle their own rendering inside the builder. You can learn more about that here.
Declaring Full Compatibility Example

Defining Custom CSS Fields For Modules

Defining Custom CSS Fields For Modules

Defining Custom CSS Fields For Modules
Learn how to define custom CSS fields for modules.

Note: This tutorial series is intended for advanced users. At least a basic understanding of coding in PHP and JavaScript is required.
Custom CSS fields appear in the Advanced tab of the module settings modal and allow users to set custom CSS properties for predefined target elements within the module』s HTML output. The fields are automatically generated based on the configuration defined in the module』s get_custom_css_fields_config() method.
Custom CSS Fields Configuration

element_one (array)

label (string) — Display name for the target element (localized)
selector (string) — CSS selector for the target element
no_space_before_selector (bool) — Don』t put a space before selector

element_two (array)

label (string) — Display name for the target element (localized)
selector (string) — CSS selector for the target element
no_space_before_selector (bool) — Don』t put a space before selector

Custom CSS Fields Configuration Example

Defining Module Settings

Defining Module Settings

Defining Module Settings
Learn how to define module settings using the field types provided by Divi.

Note: This tutorial series is intended for advanced users. At least a basic understanding of coding in PHP and JavaScript is required.
Module settings are defined in the get_fields() method of the module』s PHP class. A setting definition is simply an associative array of parameters.
Required Parameters

type (string) — The field type used to render the setting in the module settings modal

Optional Parameters

default (string) — Default value
description (string) — Description (localized)
id (string) — CSS id for the setting』s field
label (string) — Display name (localized)
option_category (string) — Option category slug (for the Divi Role Editor)
show_if (array) — Only show the setting when certain settings have certain values
show_if_not (array) — Only show the setting when certain settings do not have certain values
tab_slug (string) — Modal tab slug
toggle_slug (string) — Modal tab settings group toggle slug

Field Types
The Divi Builder has a comprehensive selection of field types for module settings. Below you』ll find a list of available field types, each with a screenshot and a list of any additional parameters that must be included in the setting definition. The value to use for the type parameter of the setting definition is in parenthesis next to each field type name.
Text (text)

Select (select)

Select Field Parameters

options (array) — Option slugs mapped to their display names (localized)

Checkboxes (multiple_checkboxes)

Checkboxes Field Parameters

options (array) — Option slugs mapped to their display names (localized)

Toggle Button (yes_no_button)

Toggle Button Field Parameters

options (array)

off (string) — Display name for the 「off」 state (localized)
on (string) — Display name for the 「on」 state (localized)

Range Slider (range)

Range Slider Field Parameters

range_settings (array)

min (string) — Minimum value
max (string) — Maximum value
step (string) — Minimum distance between values when using the slider

validate_unit (bool) — Whether or not to validate unit

How To Create A Divi Builder Module

How To Create A Divi Builder Module

How To Create A Divi Builder Module
Learn how to create a custom module for the Divi Builder.

Note: This tutorial series is intended for advanced users. At least a basic understanding of coding in PHP and JavaScript is required.
Custom Divi Builder Modules
Divi Builder modules consist of PHP, JavaScript, HTML, & CSS code. Each module is defined using a PHP class. The class defines all of the module』s settings and is responsible for rendering the module』s HTML output on the frontend. Additionally, each module has a ReactJS component class (in JavaScript) that handles rendering the module inside of the Divi Builder. In this tutorial, you』ll learn how to create a custom header module. The module will be fully functional in the builder, both on the frontend and on the backend.
Custom Divi Builder modules must be implemented within a theme, child-theme, or Divi Extension. In this tutorial we』re going to implement a custom module in a Divi Extension. If you haven』t already done so, go ahead and create a Divi Extension.
Module Definition
Divi Builder modules are defined using a PHP class. Look inside your extension』s directory and find the example module located in includes/modules. We』ll use it as a starting point to create a custom header module. First, let』s rename the HelloWorld directory to SimpleHeader. Next, rename HelloWorld.php to SimpleHeader.php, open it, and then edit it as shown below:

name = esc_html__( 'Simple Header', 'simp-simple-extension' );

}

public function get_fields() {

return array();

}

public function render( $unprocessed_props, $content = null, $render_slug ) {

}

}

new SIMP_SimpleHeader;

view raw

SimpleHeader.php

hosted with by GitHub

Our module will include just a few basic settings that can be controlled from within the Divi Builder: heading, content, and background. Module settings are defined in the get_fields() method. Let』s go ahead and do that now:

name = esc_html__( 'Simple Header', 'simp-simple-extension' );

}

public function get_fields() {

return array(

'heading' => array(

'label' => esc_html__( 'Heading', 'simp-simple-extension' ),

'type' => 'text',

'option_category' => 'basic_option',

'description' => esc_html__( 'Input your desired heading here.', 'simp-simple-extension' ),

'toggle_slug' => 'main_content',

),

'content' => array(

'label' => esc_html__( 'Content', 'simp-simple-extension' ),

'type' => 'tiny_mce',

'option_category' => 'basic_option',

'description' => esc_html__( 'Content entered here will appear below the heading text.', 'simp-simple-extension' ),

'toggle_slug' => 'main_content',

),

);

}

public function render( $unprocessed_props, $content = null, $render_slug ) {

}

}

new SIMP_SimpleHeader;

view raw

SimpleHeader.php

hosted with by GitHub

You probably noticed that the background field is missing. We excluded it from the fields array because it』s one of several advanced fields that are added automatically by the builder to all modules unless they specifically opt-out. You』ll learn more about advanced fields a bit later in this tutorial series.
Our module definition is almost complete. We just need to finish the implementation of the render() method so that it will generate the module』s HTML output based on its props. Ready? Let』s do it!

name = esc_html__( 'Simple Header', 'simp-simple-extension' );

}

public function get_fields() {

return array(

'heading' => array(

'label' => esc_html__( 'Heading', 'simp-simple-extension' ),

'type' => 'text',

'option_category' => 'basic_option',

'description' => esc_html__( 'Input your desired heading here.', 'simp-simple-extension' ),

'toggle_slug' => 'main_content',

),

'content' => array(

'label' => esc_html__( 'Content', 'simp-simple-extension' ),

'type' => 'tiny_mce',

'option_category' => 'basic_option',

'description' => esc_html__( 'Content entered here will appear below the heading text.', 'simp-simple-extension' ),

'toggle_slug' => 'main_content',

),

);

}

public function render( $unprocessed_props, $content = null, $render_slug ) {

return sprintf(

'

%1$s

%2$s

',

esc_html( $this->props['heading'] ),

$this->props['content']

);

}

}

new SIMP_SimpleHeader;

view raw

SimpleHeader.php

hosted with by GitHub

React Component
In order for our module to be available and fully functional inside the Divi Builder we must create a React Component class that handles the rendering of our module based on its props. Look in your module』s directory for the file named HelloWorld.jsx.
Note: JSX is a syntax extension to JavaScript used in React to describe what the UI should look like.
Let』s rename HelloWorld.jsx to SimpleHeader.jsx, open it, and then edit it as follows:

// External Dependencies

import React, { Component, Fragment } from 'react';

// Internal Dependencies

import './style.css';

class SimpleHeader extends Component {

static slug = 'simp_simple_header';

render() {

return (

{this.props.content()}

);

}

}

export default SimpleHeader;

view raw

SimpleHeader.jsx

hosted with by GitHub

Next, let』s update the import and export statements in includes/modules/index.js:

// Internal Dependencies

import SimpleHeader from './SimpleHeader/SimpleHeader';

export default [

SimpleHeader,

];

view raw

index.js

hosted with by GitHub

Now, let』s edit the render() method and make it produce the same output that we defined in our PHP render() method.

// External Dependencies

import React, { Component, Fragment } from 'react';

// Internal Dependencies

import './style.css';

class SimpleHeader extends Component {

static slug = 'simp_simple_header';

render() {

return (

{this.props.heading}

{this.props.content()}

);

}

}

export default SimpleHeader;

view raw

SimpleHeader.jsx

hosted with by GitHub

There are two things in our render() method to take note of. First, note how the module』s content setting is handled. Module settings defined with field type tiny_mce (like the content setting in our module) require the use of a special React Component. The builder sets up the required component and then passes it down to the module as the setting value. Since the value is not a string or number and is actually a React Component, we need to use it as such in our JSX markup.
Also note how we wrapped our module』s output in a Fragment component. Fragments allow you to return multiple top-level elements from your render() method without actually adding an extra element to the page itself.
CSS Styles
Styles for our module can be defined using the style.css file in its directory. Our custom header module is pretty basic so it doesn』t require much styling. Though we should add some bottom margin for the heading so that there is some space between it and the content below it. Later, in our Divi Builder Module In-Depth tutorial series you』ll learn how to make margin and padding for the heading (or any element inside your module』s output) configurable from within the module』s settings.
For now, let』s go ahead and update our module』s style.css:

.simp-simple-header-heading {

margin-bottom: 20px;

}

view raw

style.css

hosted with by GitHub

Testing During Development
Before we can test our custom module in the Divi Builder we need to compile the JSX code into regular JavaScript. To do that, simply run the following command inside your plugin』s directory:
yarn start

Provided there are no syntax errors in your code you will see the following output:

Now you can launch the Divi Builder and check out your Simple Header module!
 
Note: You must keep the terminal window with yarn start running open while you are developing your module. As you make changes to the files, the JavaScript and CSS will be recompiled automatically.

Module Settings Groups

Module Settings Groups

Module Settings Groups
Learn how to configure the toggle groups in the module settings modal.

Note: This tutorial series is intended for advanced users. At least a basic understanding of coding in PHP and JavaScript is required.
When editing modules inside the Divi Builder, their settings appear in collapsable groups . You can assign a setting to a specific group using the toggle_slug parameter in the setting definition. You can use one of the builder』s predefined settings groups or you can use a custom group.
Predefined Settings Groups
The following groups are predefined and can be used by simply including one of their slugs as the toggle_slug parameter in a setting definition.

Display Name
Slug

Admin Label
admin_label

Alignment
alignment

Animation
animation

Arrows Color
arrows_color

Attributes
attributes

Audio
audio

Background
background

Bar Counter
bar

Body Text
body

Border
border

Box Shadow
box_shadow

Bullet
bullet

Button One
button_one

Button Two
button_two

Button
button

CSS ID & Classes
classes

Caption Text
caption

Categories
categories

Circle
circle

Color
color

Conditional Logic
conditional_logic

Content Text
content

Controls Colors
colors

Controls
controls

Currency & Frequency Text
currency_frequency

Custom CSS
custom_css

Dividers
dividers

Dropdown Menu
dropdown

Elements
elements

Email Account
provider

Email
email

Exceptions
exceptions

Excluded Item
excluded

Featured Image
featured_image

Field Options
field_options

Field Text
form_field

Fields
fields

Filter Criteria Text
filter

Filters
filters

Icon
icon

Image & Icon
icon_settings

Image & Video
image_video

Images
images

Image
image

Input Text
input

Label Text
label

Layout
layout

Links
links

Link
link

Map
child_filters

Map
map

Menu Text
menu

Meta Text
meta

Module Text
module

Navigation
navigation

Number Text
number

Numbers Text
numbers

Overlay
overlay

Pagination Text
pagination

Percentage Text
percent

Play Icon
play_icon

Player Pause
player_pause

Portfolio Title Text
portfolio_header

Price Text
price

Redirect
redirect

Result Message Text
result_message

Rotation
rotation

Sale Badge
badge

Scroll Down Icon
scroll_down

Search Field
field

Sizing
width

Spacing
margin_padding

State
state

Styles
styles

Subhead Text
subhead

Subheader Text
subheader

Success Action
success_action

Tab Text
tab

Text
main_content

Text
text

Title Text
header

Title Text
title

Toggle Text
toggle

Visibility
visibility

Custom Settings Groups
Settings can be assigned to a custom group in the same way that you would assign them to predefined groups. However, you must also define all custom groups in the get_settings_modal_toggles() method of your module』s PHP class.
Settings Group Definition

tab_slug (array)

toggles (array) — All settings group definitions for the tab

toggle_slug (array) — Settings group definition

priority (int) — Groups are sorted based on this number (from lowest to highest)
sub_toggles (array) — Sub groups for this group (optional)

sub_toggle_slug (array) — Sub group definition

icon (string) — The slug of a predefined icon (optional)
icon_svg (string) — Raw SVG icon (optional)
name (string) — Display Name (only shown when no icon is defined)

tabbed_subtoggles (bool) — Whether or not to display sub groups as tabs
title (string) — Display name (localized)

Custom Settings Groups Example

array(

'label' => esc_html__('Add Mushroom?', 'simp-simple-extension'),

'type' => 'yes_no_button',

'option_category' => 'basic_option',

'description' => esc_html__('Toggle whether mushroom will be added to the pizza.', 'simp-simple-extension'),

'toggle_slug' => 'pizza',

'options' => array(

'on' => esc_html__( 'Yes', 'simp-simple-extension'),

'off' => esc_html__( 'No', 'simp-simple-extension'),

),

'sub_toggle' => 'pizza_toppings_mushroom',

),

'quantity_toppings_mushroom' => array(

'label' => esc_html__('How Much Mushroom?', 'simp-simple-extension'),

'type' => 'range',

'option_category' => 'basic_option',

'description' => esc_html__('Dropdown menu with various options.', 'simp-simple-extension'),

'toggle_slug' => 'pizza',

'range_settings' => array(

'min' => '1',

'max' => '5',

'step' => '1',

),

'show_if' => array(

'toggle_toppings_mushroom' => 'on',

),

'sub_toggle' => 'pizza_toppings_mushroom',

),

'toggle_toppings_anchovy' => array(

'label' => esc_html__('Add Anchovies?', 'simp-simple-extension'),

'type' => 'yes_no_button',

'option_category' => 'basic_option',

'description' => esc_html__('Toggle whether anchovies will be added to the pizza.', 'simp-simple-extension'),

'toggle_slug' => 'pizza',

'options' => array(

'on' => esc_html__( 'Yes', 'simp-simple-extension'),

'off' => esc_html__( 'No', 'simp-simple-extension'),

),

'sub_toggle' => 'pizza_toppings_anchovy',

),

'quantity_toppings_anchovy' => array(

'label' => esc_html__('How Many Anchovies?', 'simp-simple-extension'),

'type' => 'range',

'option_category' => 'basic_option',

'description' => esc_html__('Dropdown menu with various options.', 'simp-simple-extension'),

'toggle_slug' => 'pizza',

'range_settings' => array(

'min' => '1',

'max' => '30',

'step' => '1',

),

'show_if' => array(

'toggle_toppings_anchovy' => 'on',

),

'sub_toggle' => 'pizza_toppings_anchovy',

),

'toggle_toppings_pineapple' => array(

'label' => esc_html__('Add Pineapple?', 'simp-simple-extension'),

'type' => 'yes_no_button',

'option_category' => 'basic_option',

'description' => esc_html__('Toggle whether pineapple will be added to the pizza.', 'simp-simple-extension'),

'toggle_slug' => 'pizza',

'options' => array(

'on' => esc_html__( 'Yes', 'simp-simple-extension'),

'off' => esc_html__( 'No', 'simp-simple-extension'),

),

'default' => 'on',

'sub_toggle' => 'pizza_toppings_pineapple',

),

'quantity_toppings_pineapple' => array(

'label' => esc_html__('How Much Pineapple?', 'simp-simple-extension'),

'type' => 'range',

'option_category' => 'basic_option',

'description' => esc_html__('Dropdown menu with various options.', 'simp-simple-extension'),

'toggle_slug' => 'pizza',

'range_settings' => array(

'min' => '1',

'max' => '9001',

'step' => '1',

),

'default' => '9001',

'show_if' => array(

'toggle_toppings_pineapple' => 'on',

),

'sub_toggle' => 'pizza_toppings_pineapple',

),

'toppings_other' => array(

'label' => esc_html__('Content', 'simp-simple-extension'),

'type' => 'text',

'option_category' => 'basic_option',

'description' => esc_html__('Add any special requests for toppings not in the list.', 'simp-simple-extension'),

'toggle_slug' => 'pizza',

'sub_toggle' => 'pizza_toppings_other',

),

);

}

public function get_settings_modal_toggles() {

return array(

'advanced' => array(

'toggles' => array(

'pizza' => array(

'priority' => 24,

'sub_toggles' => array(

'pizza_toppings_mushroom' => array(

'name' => 'Mushroom',

'icon_svg' => '',

),

'pizza_toppings_anchovy' => array(

'name' => 'Anchovies',

'icon_svg' => '',

),

'pizza_toppings_pineapple' => array(

'name' => 'Pineapple',

'icon_svg' => '',

),

'pizza_toppings_other' => array(

'name' => 'Other',

'icon' => 'text-quote',

),

),

'tabbed_subtoggles' => true,

'title' => 'Special Toppings',

),

),

),

);

}

...

view raw

settings-groups--custom-settings-groups.php

hosted with by GitHub