Kis*_*int 17 php wordpress plugins class
如何为Divi Wordpress主题添加自定义模块? http://www.elegantthemes.com/gallery/divi/
原始模块在main-modules.php中创建
例:
class ET_Builder_Module_Gallery extends ET_Builder_Module { .... }
Run Code Online (Sandbox Code Playgroud)
但是ET_Builder_Module我的插件或主题functions.php中无法访问该类
Phi*_*ipp 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;
// ...
}
}
}
Run Code Online (Sandbox Code Playgroud)
你可以在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();
}
Run Code Online (Sandbox Code Playgroud)
Ote*_*rox -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';
Run Code Online (Sandbox Code Playgroud)
之后,您需要在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' => '',
) );
Run Code Online (Sandbox Code Playgroud)
如果您在functions.php中进行搜索,您将找到轻松添加它的位置;)