1

In popular plugins, like WooCoommerce, a developer can copy the template files into a [child] theme to override files. I believe this is possible with Beaver Builder as well with some of their modules. I've tried to look for how this happens or how the files are loaded within these plugins and elsewhere on the web, but have been unsuccessful.

My first question is, what is the function or loader that makes this happen? And secondly, is this a global Wordpress feature, or do plugin developers have to build this functionality into the plugin?

1 Answer 1

3

My first question is, what is the function or loader that makes this happen? And secondly, is this a global Wordpress feature, or do plugin developers have to build this functionality into the plugin?

It's not a WordPress feature. Not exactly. Each plugin that does this has created their own function for loading template files that checks the theme for template files before loading them from the plugin.

In WooCommerce this is the wc_locate_template() function. This function uses locate_template() to look in the child theme and theme parent for a given file, and if it finds it, it loads it, otherwise it falls back to the file in its own template directory. Internally, WooCommerce uses this function whenever it loads templates, which allows themes to replace the template files. Beaver Builder likely has a very similar function.

A very simple (untested) version of a function like this would be:

function wpse_336136_load_template( $template_name ) {
    $template_name = ltrim( $template_name, '/' );
    $template_path = locate_template( 'my-plugin/' . $template_name );

    if ( ! $template_path ) {
        $template_path = plugin_dir_path( 'templates/' . $template_name, __FILE__ );
    }

    return $template_path;
}

So now the function wpse_336136_load_template() will check the themes for a given file, inside the my-plugin/ directory, and if it doesn't find it, it will load it from the templates/ directory of the plugin.

For example, if you use:

$template_path = wpse_336136_load_template( 'partials/header.php' )

It will check these directories, in this order:

wp-content/themes/child-theme/my-plugin/partials/header.php 
wp-content/themes/parent-theme/my-plugin/partials/header.php 
wp-content/plugins/my-plugin/templates/partials/header.php 

So as long as you used wpse_336136_load_template() internally in your plugin, users would be able to replace any file in your plugin's templates/ directory by copying the files to the my-plugin/ directory of their theme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.