Enqueue Module Style
Objective
The goal of this tutorial is to help you enqueue static style for your module.
Prerequisites
This tutorial assumes that you have read, followed, and completed:
Enqueue style via module.json
To enqueue static style for your module, one way to do it is:
- Create static style
- Create function that enqueues that static style using
wp_enqueue_style() - Add the function to
wp_enqueue_scriptshook
That will do the job, but that's not what we are recommending. Essentially, Divi 5 module is saved using serialized block format (Gutenberg's / WordPress Block Editor's saved format) which will be processed by WordPress' block parser. Divi 5's module's module.json is essentially WordPress block's block.json with lots of additional Divi specific properties. Therefore, properties that works in block.json (in general) will also work in module.json.
One of them is style property that can be used to declare static style for your module.
How It Works
Create static style
First, we'll create this static style:
.d5_tut_simple_quick_module {
padding: 30px;
}
.d5_tut_simple_quick_module_title {
font-size: 50px;
}
Register static style
Next, we will register the static style on d5-tutorial-simple-quick-module.php file:
/**
* Register Module Assets
*/
function d5_tutorial_simple_quick_module_assets() {
wp_register_style(
'd5-tutorial-simple-quick-module-style',
D5_TUTORIAL_SIMPLE_QUICK_MODULE_URL . 'style.css',
[]
);
}
add_action( 'wp_enqueue_scripts', 'd5_tutorial_simple_quick_module_assets' );
At this point, you'll have a question: why register instead of enqueue it? The reason:
Declare style on module.json
Instead of manually enqueueing static style, what we'll do instead is to declare the style on style property on module.json. Here's how you do it; look for the highlighted part:
{
"name": "d5-tut/simple-quick-module",
"d4Shortcode": "d5_tut_simple_quick_module",
"title": "Simple Quick Module",
"titles": "Simple Quick Modules",
"category": "module",
"attributes": {
"module": {
"type": "object",
"selector": "{{selector}}",
"default": {
"meta": {
"adminLabel": {
"desktop": {
"value": "Simple Quick Module"
}
}
}
}
},
"title": {
"type": "object",
"selector": "{{selector}} .d5_tut_simple_quick_module_title",
"attributes": {
"class": "d5_tut_simple_quick_module_title"
},
"tagName": "h2",
"inlineEditor": "plainText",
"elementType": "heading",
"childrenSanitizer": "et_core_esc_previously"
},
"content": {
"type": "object",
"selector": "{{selector}} .d5_tut_simple_quick_module_content",
"attributes": {
"class": "d5_tut_simple_quick_module_content"
},
"tagName": "div",
"inlineEditor": "richText",
"childrenSanitizer": "et_core_esc_previously",
"allowHtml": true
}
},
"style": ["d5-tutorial-simple-quick-module-style"]
}
By doing this WordPress and its block parser will automatically enqueue the static style on every page. Here is the catch: WordPress actually have a mechanism for only enqueueing style that is being used on the page. However, it is disabled by default so all module / block styles are enqueued on fronted by default at all time.
To only enqueue style that is being used on this page, this filtered value of separate_core_block_assets has to return true. In a nutshell, if you want to do it, this will do:
add_filter( 'separate_core_block_assets', '__return_true' );
This will only enqueue module static style when the module is being rendered on the page. That being said, this requires us to do the following:
Always enqueue static style visual builder
If separate_core_block_assets is set to true and you are opening new visual builder page with no saved content, it means module's static style won't be enqueued. Thus, we'll need to ensure that static style is manually enqueued on visual builder. See the highlighted part:
/**
* Enqueue Divi 5 Visual Builder Assets
*/
function d5_tutorial_simple_quick_module_enqueue_visual_builder_assets() {
if ( et_core_is_fb_enabled() && et_builder_d5_enabled() ) {
wp_enqueue_script(
'd5-tutorial-simple-quick-module-visual-builder',
D5_TUTORIAL_SIMPLE_QUICK_MODULE_URL . 'visual-builder/build/d5-tutorial-simple-quick-module.js',
[
'react',
'jquery',
'divi-module-library',
'wp-hooks',
'divi-rest',
],
'1.0.0',
true
);
}
// Ensure module style will be enqueued on Visual Builder even when `should_load_separate_core_block_assets`
// filter returns `true` which means module style is only being enqueued when module is saved on the page.
wp_enqueue_style( 'd5-tutorial-simple-quick-module-style' );
}
Got a question or feedback? Ask away!