Created
June 12, 2019 13:41
-
-
Save DevWael/ed562e4dbab90356596e662d417278cc to your computer and use it in GitHub Desktop.
Override Woocommerce templates from a WordPress plugin
This file contains 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 | |
add_filter( 'woocommerce_locate_template', 'inno_oc_woocommerce_locate_template', 10, 3 ); | |
function inno_oc_woocommerce_locate_template( $template, $template_name, $template_path ) { | |
global $woocommerce; | |
$_template = $template; | |
if ( ! $template_path ) { | |
$template_path = $woocommerce->template_url; | |
} | |
$plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/woocommerce/'; | |
// Look within passed path within the theme - this is priority | |
$template = locate_template( | |
array( | |
$template_path . $template_name, | |
$template_name | |
) | |
); | |
// Modification: Get the template from this plugin, if it exists | |
if ( ! $template && file_exists( $plugin_path . $template_name ) ) { | |
$template = $plugin_path . $template_name; | |
} | |
// Use default template | |
if ( ! $template ) { | |
$template = $_template; | |
} | |
// Return what we found | |
return $template; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment