繁体   English   中英

如何创建自定义Divi模块?

[英]How to create custom Divi module?

如何为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

这里的大多数其他解决方案都太复杂了。 最简单的方法是在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/

将下面放在functions.php文件中。 include文件(我称之为custom-modules.php)将是一个扩展ET_Builder_Module的类(与WP_Widget非常相似)。 只需从Divi >> includes >> 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();
}

上面的代码不起作用该函数也需要调用。

以下是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();

重要说明:自定义模块的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);

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

我想尝试解决这里的小辩论。 类ET_Builder_Module_Custom_Module扩展ET_Builder_Module {}实际上工作,并且如果使用子主题,可以自由修改main-modules.php。 我最近ajaxified一个Divi主题,更新后一切都像一个魅力。

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

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

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

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

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

你可以修改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中搜索,你会发现在哪里轻松添加;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM