Over write plugin templates — Child plugin concept

Is that possible for overwriting a plugin templates via making a folder in theme file and copy the file in that folder and altering the content [both folder and file name have the same name as arranged in plugin ].

Generally we altering the woocommerce page templates by following these system .Woothemes has implemented something similar for their woocommerce plugin. You copy files from /wp-content/plugins/woocommerce <–>to<–> /wp-content/themes/yourthemes/woocommerce and WP automatically uses the files in the theme folder rather than the plugin folder. This way users can make customizations to their plugins without losing them to a plugin update..Is that possible for all plugin ?

Read More

If no , what code that make woocommerce plugin to change the style based on the file inside our theme folders woocoomerce folder ?

or What about CHILD PLUGIN concept ?

is there any possible way ?

Related posts

1 comment

  1. Woocommerce checks first does file /wp-content/themes/yourthemes/woocommerce exists and reqire it. If not it require general template from /wp-content/plugins/woocommerce

    Simple possible solution in your plugin, you can use.

    function loadTemplate( $template_name ){
        $plugin_path = plugin_dir_path(__FILE__);
        $original_template = $plugin_path . "templates/" . $template_name . ".php";
    
        $theme_path = get_template_directory();
        $override_template = $theme_path . "/myplugin/" . $template_name . ".php";
    
        if(file_exists($override_template)){
             include( $override_template );
        }
        else{
             include( $original_template );
        }
    }
    

    And now you can use as you want your loadTemplate() function like so:

    function load_teplate_after_content( $template_name ) {
        loadTemplate( 'example' );
        // Now it will check first for
        // wp-content/themes/yourtheme/myplugin/example.php 
        // and load, if not exists it will load original template from
        // /wp-content/plugins/myplugin/templates/example.php
    }
    add_filter( 'the_content', 'load_teplate_after_content' );
    

    Of course remember to prefix your functions or put it in a Class. This is just simple example.

    Not tested. May be some errors.

    EDIT:

    Just to answer all questions. Here is precisely how woocommerce is doing this

    /**
     * Get template part (for templates like the shop-loop).
     *
     * @access public
     * @param mixed $slug
     * @param string $name (default: '')
     * @return void
     */
    function wc_get_template_part( $slug, $name = '' ) {
        $template = '';
    
        // Look in yourtheme/slug-name.php and yourtheme/woocommerce/slug-name.php
        if ( $name && ! WC_TEMPLATE_DEBUG_MODE ) {
            $template = locate_template( array( "{$slug}-{$name}.php", WC()->template_path() . "{$slug}-{$name}.php" ) );
        }
    
        // Get default slug-name.php
        if ( ! $template && $name && file_exists( WC()->plugin_path() . "/templates/{$slug}-{$name}.php" ) ) {
            $template = WC()->plugin_path() . "/templates/{$slug}-{$name}.php";
        }
    
        // If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php
        if ( ! $template && ! WC_TEMPLATE_DEBUG_MODE ) {
            $template = locate_template( array( "{$slug}.php", WC()->template_path() . "{$slug}.php" ) );
        }
    
        // Allow 3rd party plugin filter template file from their plugin
        if ( ( ! $template && WC_TEMPLATE_DEBUG_MODE ) || $template ) {
            $template = apply_filters( 'wc_get_template_part', $template, $slug, $name );
        }
    
        if ( $template ) {
            load_template( $template, false );
        }
    }
    

    EDIT2:

    Is that possible for all plugin

    It’s possible for all plugins which have this feature implemented.

    or What about CHILD PLUGIN concept ?

    There is no general answer. If plugin provide an API to its functionallities, you could create such child plugin. But depending directly on current plugin code is bad idea. When your parent plugin(1) is gonna change, your plugin will break if you are using function that has been removed or edited.

    But as I said, if plugin provide consistent API, and it’s enough to write such functionaliity, then yes. But not for all plugins. At least without editing plugins itself.

    (1) There is no such thing like child plugin or parent plugin oficially.

Comments are closed.