class.swpm-utils-template.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. class SwpmUtilsTemplate {
  3. /*
  4. * This function will load the template file in the following order
  5. * wp-content/themes/your-child-theme/simple-membership/template-name.php
  6. * wp-content/themes/your-main-theme/simple-membership/template-name.php
  7. * The standard plugin's template file
  8. */
  9. public static function swpm_load_template($template_name, $require_once = true) {
  10. //List of file paths (in order of priority) where the plugin should check for the template.
  11. $template_files = array(
  12. get_stylesheet_directory() . '/' . SIMPLE_WP_MEMBERSHIP_TEMPLATE_PATH . '/' . $template_name, //First check inside child theme (if you are using a child theme)
  13. get_template_directory() . '/' . SIMPLE_WP_MEMBERSHIP_TEMPLATE_PATH . '/' . $template_name, //Then check inside the main theme folder
  14. SIMPLE_WP_MEMBERSHIP_PATH . 'views/' . $template_name //Otherwise load the standard template
  15. );
  16. //Filter hook to allow overriding of the template file path
  17. $template_files = apply_filters( 'swpm_load_template_files', $template_files, $template_name);
  18. foreach ($template_files as $file) {
  19. if (file_exists($file)) {
  20. $template_to_load = $file;
  21. break;
  22. }
  23. }
  24. //Lets load this template
  25. if ($template_to_load) {
  26. if ($require_once) {
  27. require_once( $template_to_load );
  28. } else {
  29. require( $template_to_load );
  30. }
  31. } else {
  32. wp_die(SwpmUtils::_('Error! Failed to find a template path for the specified template: ' . $template_name));
  33. }
  34. }
  35. }