-
-
Save manidip/e438ec2eab478e4841a62b9b4afdc21a to your computer and use it in GitHub Desktop.
Override WooCommerce template files via a plugin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * WooCommerce Plugin Template Loader | |
| * Override WooCommerce template files within a custom plugin folder | |
| * http://gist.github.com/danchivz | |
| * | |
| * Licensed under the MIT license. | |
| */ | |
| /** | |
| * To override templates within a plugin, two filters are provided by WooCommerce, ' woocommerce_locate_template' and 'wc_get_template_part'. | |
| * Create a subfolder named 'woocommerce' inside the plugin folder, and place there custom templates. | |
| * Templates will be loaded in the following hierarchy: | |
| * theme/template_path/template_name | |
| * theme/template_name | |
| * plugin/template_path/template_name | |
| * default/template_name | |
| */ | |
| // get path for templates used in loop | |
| add_filter( 'wc_get_template_part', function( $template, $slug, $name ) | |
| { | |
| // look in plugin/woocommerce/slug-name.php or plugin/woocommerce/slug.php | |
| if ( $name ) { | |
| $path = plugin_dir_path( __FILE__ ) . WC()->template_path() . "{$slug}-{$name}.php"; | |
| } else { | |
| $path = plugin_dir_path( __FILE__ ) . WC()->template_path() . "{$slug}.php"; | |
| } | |
| return file_exists( $path ) ? $path : $template; | |
| }, 10, 3 ); | |
| // get path for all other templates. | |
| add_filter( 'woocommerce_locate_template', function( $template, $template_name, $template_path ) | |
| { | |
| $path = plugin_dir_path( __FILE__ ) . $template_path . $template_name; | |
| return file_exists( $path ) ? $path : $template; | |
| }, 10, 3 ); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment