如何创建自定义Divi模块?

时间:2015-07-22 13:56:49

标签: php wordpress class plugins

如何为Divi Wordpress主题添加自定义模块? http://www.elegantthemes.com/gallery/divi/

原始模块在 main-modules.php

中创建

示例:

class ET_Builder_Module_Gallery extends ET_Builder_Module { .... }

但我的插件或主题 functions.php

无法访问ET_Builder_Module

7 个答案:

答案 0 :(得分:11)

这里的大多数其他解决方案都过于复杂。最简单的方法是在divi特定操作挂钩et_builder_ready中加载自定义模块,如下所示:

add_action( 'et_builder_ready', 'evr_initialize_divi_modules' );

function evr_initialize_divi_modules() {
    if ( ! class_exists( 'ET_Builder_Module' ) ) { return; }

    class EVR_Builder_Module_Testimonial extends ET_Builder_Module {
        function init() {
            $this->name       = esc_html__( 'Testimonial', 'evr' );
            $this->slug       = 'evr_pb_testimonial';
            $this->fb_support = true;

            // ...

        }
    }
}

您可以在github上找到完整的演示代码。除了一些说明如何让它在全新的Divi 3前端构建器中运行:

https://github.com/stracker-phil/divi3-vb-custom-modules/

答案 1 :(得分:6)

将以下内容放在functions.php文件中。 include文件(我称之为custom-modules.php)将是一个扩展ET_Builder_Module的类(与WP_Widget非常相似)。只需打开Divi>>包含>> builder>> main-modules.php的文件即可。使用任何预先存在的模块作为新模块的示例或基础。复制并粘贴到custom-modules.php中。新的类名,根据需要进行编辑等。

function doCustomModules(){
 if(class_exists("ET_Builder_Module")){
    include("custom-modules.php");
 }
}

function prepareCustomModule(){
 global $pagenow;

 $is_admin = is_admin();
 $action_hook = $is_admin ? 'wp_loaded' : 'wp';
 $required_admin_pages = array( 'edit.php', 'post.php', 'post-new.php', 'admin.php', 'customize.php', 'edit-tags.php', 'admin-ajax.php', 'export.php' ); // list of admin pages where we need to load builder files
 $specific_filter_pages = array( 'edit.php', 'admin.php', 'edit-tags.php' ); // list of admin pages where we need more specific filtering
 $is_edit_library_page = 'edit.php' === $pagenow && isset( $_GET['post_type'] ) && 'et_pb_layout' === $_GET['post_type'];
    $is_role_editor_page = 'admin.php' === $pagenow && isset( $_GET['page'] ) && 'et_divi_role_editor' === $_GET['page'];
    $is_import_page = 'admin.php' === $pagenow && isset( $_GET['import'] ) && 'wordpress' === $_GET['import']; // Page Builder files should be loaded on import page as well to register the et_pb_layout post type properly
    $is_edit_layout_category_page = 'edit-tags.php' === $pagenow && isset( $_GET['taxonomy'] ) && 'layout_category' === $_GET['taxonomy'];

 if ( ! $is_admin || ( $is_admin && in_array( $pagenow, $required_admin_pages ) && ( ! in_array( $pagenow, $specific_filter_pages ) || $is_edit_library_page || $is_role_editor_page || $is_edit_layout_category_page || $is_import_page ) ) ) {
    add_action($action_hook, 'doCustomModules', 9789);
 }
}
$theme_data = wp_get_theme();
$parent_data = $theme_data->parent();
if(version_compare((string)$parent_data->Version, "2.5.9", ">")) {
    add_action('et_builder_ready', 'doCustomModules');
} else {
    doCustomModule();
}

答案 2 :(得分:3)

上面的代码没有用 该函数也需要调用。

以下是来自https://divi.space/blog/adding-custom-modules-to-divi/

的工作代码的示例
function DS_Custom_Modules(){
 if(class_exists("ET_Builder_Module")){
 include("ds-custom-modules.php");
 }
}

function Prep_DS_Custom_Modules(){
 global $pagenow;

$is_admin = is_admin();
 $action_hook = $is_admin ? 'wp_loaded' : 'wp';
 $required_admin_pages = array( 'edit.php', 'post.php', 'post-new.php', 'admin.php', 'customize.php', 'edit-tags.php', 'admin-ajax.php', 'export.php' ); // list of admin pages where we need to load builder files
 $specific_filter_pages = array( 'edit.php', 'admin.php', 'edit-tags.php' );
 $is_edit_library_page = 'edit.php' === $pagenow && isset( $_GET['post_type'] ) && 'et_pb_layout' === $_GET['post_type'];
 $is_role_editor_page = 'admin.php' === $pagenow && isset( $_GET['page'] ) && 'et_divi_role_editor' === $_GET['page'];
 $is_import_page = 'admin.php' === $pagenow && isset( $_GET['import'] ) && 'wordpress' === $_GET['import']; 
 $is_edit_layout_category_page = 'edit-tags.php' === $pagenow && isset( $_GET['taxonomy'] ) && 'layout_category' === $_GET['taxonomy'];

if ( ! $is_admin || ( $is_admin && in_array( $pagenow, $required_admin_pages ) && ( ! in_array( $pagenow, $specific_filter_pages ) || $is_edit_library_page || $is_role_editor_page || $is_edit_layout_category_page || $is_import_page ) ) ) {
 add_action($action_hook, 'DS_Custom_Modules', 9789);
 }
}
Prep_DS_Custom_Modules();

答案 3 :(得分:3)

重要说明:自定义模块的slug必须包含字符串et_pb_,否则它将被函数et_pb_allowed_modules_list()过滤掉

我能够使用以下代码添加新的Divi模块(匿名函数需要PHP 5.3+):

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

在包含的文件中,复制并粘贴class文件中的wp-content/themes/Divi/includes/builder/main-modules.php,然后根据您的需要进行修改。请参阅下面的示例(从提到的文件中复制一个实际的类以获取下面列出的每个方法的内容...为了简单起见,我喜欢ET_Builder_Module_Code类):

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;

答案 4 :(得分:2)

我想尝试解决这里的小辩论。 类ET_Builder_Module_Custom_Module扩展ET_Builder_Module {}实际上可以工作,main-modules.php可以自由修改使用子主题。 我最近对一个Divi主题进行了调整,在更新之后,所有内容都像魅力一样。

注意:检查您在子主题中使用的文件是否有更新是一个好主意,因为有时您可能还需要更新子文件。

我希望这有助于这篇文章的所有未来读者。

带有您即将创建的新模块的HFGL;)

答案 5 :(得分:0)

对于创建自定义模块,我将使用现有模块来创建设计(布局),将该设计保存到Divi库中,并使用Text或Code模块,并在其中编写所有HTML / jquery代码。

像这里https://github.com/Skaidrius/Divi/tree/master/Layouts/Price-list

答案 6 :(得分:-2)

你可以修改Divi主题文件(这是不好的)

例如,您可以修改main-modules.php以添加新模块:

class ET_Builder_Module_Custom_Module extends ET_Builder_Module {
    function init() {
        $this->name = __( 'My Module', 'et_builder' );
        $this->slug = 'et_pb_custom_module';

之后,您需要在functions.php中添加自定义程序面板:

/* Section: Custom Module */
$wp_customize->add_section( 'et_pagebuilder_custom_module', array(
    'priority'       => 220,
    'capability'     => 'edit_theme_options',
    'title'          => __( 'My Module', 'Divi' ),
    // 'description'    => '',
) );

如果你在functions.php中进行搜索,你会找到容易添加的地方;)