Search code examples
phpwordpresswordpress-themingdivi

How to get child theme module templates to override main module template


For some reason my child theme templates are not being recognized.

I believe I have followed the correct procedure (and checked for caching etc)

/wp-content/themes/divi-child/includes/builder/module/Blog.php

should replace

/wp-content/themes/Divi/includes/builder/module/Blog.php

(same path and same file with a slight update)

The child theme module template is not recognized (changes to the child theme template have no effect)

I have tested editing the main file and this works immediately every time.

Any advice greatly appreciated.

Cheers


EDIT

The below should work according to Divi however it breaks the site when I try.

Apparently it is not enough to just copy the module into the child theme. The file needs to be duplicated. Then copied file to child-theme/custom-modules/Blog.php. After adding following code to the bottom of the functions.php file:

function divi_custom_blog_module() {
get_template_part( '/custom-modules/Blog' );
$myblog = new custom_ET_Builder_Module_Blog();
remove_shortcode( 'et_pb_blog' );
add_shortcode( 'et_pb_blog', array( $myblog, '_render' ) );
}
add_action( 'et_builder_ready', 'divi_custom_blog_module' );

Solution

  • There are a few other steps https://intercom.help/elegantthemes/en/articles/4532734-moving-blog-module-in-child-theme

    1. Create a new folder in the child theme folder, for example, includes folder.

    2. Now copy the Divi/includes/builder/module/Blog.php file from the parent theme into the child-theme/includes/ folder.

    3. Open up the Blog.php file of your child theme and replace this line (at the very top):

        require_once 'helpers/Overlay.php';
            
        class ET_Builder_Module_Blog extends ET_Builder_Module_Type_PostBased {
    

    with:

        get_template_part( '/includes/builder/module/helpers/Overlay.php' );
        
        class custom_ET_Builder_Module_Blog extends ET_Builder_Module_Type_PostBased {
    

    Replace: $this->vb_support = 'on'; with $this->vb_support = 'off';

    Remove this line from the bottom: new ET_Builder_Module_Blog();

    1. Finally, add the following code to the functions.php file in your child theme folder:
    
    /*================================================
    
    #Load custom Blog  Module
    
    ================================================*/
    
    function divi_custom_blog_module() {
    
        get_template_part( '/includes/Blog' );
    
        $myblog = new custom_ET_Builder_Module_Blog();
    
        remove_shortcode( 'et_pb_blog' );
    
        add_shortcode( 'et_pb_blog', array( $myblog, '_render' ) );
    
    }
    
    add_action( 'et_builder_ready', 'divi_custom_blog_module' );