How to create custom Divi module?

2019-03-12 19:48发布

How can I add a custom module for Divi Wordpress theme? http://www.elegantthemes.com/gallery/divi/

Original modules are created in main-modules.php

Example:

class ET_Builder_Module_Gallery extends ET_Builder_Module { .... }

But the ET_Builder_Module class is not accessible in my plugin, or in theme functions.php

7条回答
放我归山
2楼-- · 2019-03-12 20:45

Important note: The slug for your custom module must contain the string et_pb_, or it will be filtered out by the function et_pb_allowed_modules_list().

I was able to add a new Divi module using the following code (requires PHP 5.3+ for the anonymous function):

add_action(is_admin() ? 'wp_loaded' : 'wp', function() {
  require __DIR__ . '/custom-divi-module.php';
}, 20);

Inside the included file, copy and paste a class from the wp-content/themes/Divi/includes/builder/main-modules.php file, then modify to suit your needs. See the example below (copy an actual class from the file mentioned to get the content of each method listed below… I like the ET_Builder_Module_Code class for simplicity's sake):

class YOUR_MODULE_NAME extends ET_Builder_Module {
  function init() {
    // Name, slug, and some other settings for the module go here
  }

  function get_fields() {
    // This method returns an array of fields that the module will
    // display as the module settings
  }

  function shortcode_callback($atts, $content = null, $function_name) {
    // This method returns the content the module will display
  }
}
new YOUR_MODULE_NAME;
查看更多
登录 后发表回答