Last active
October 21, 2023 13:36
-
-
Save ashokmhrj/b5f6e28f15dc84601954 to your computer and use it in GitHub Desktop.
Get Template Part From plugin directory
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 | |
/** | |
* The below function will help to load template file from plugin directory of wordpress | |
* Extracted from : http://wordpress.stackexchange.com/questions/94343/get-template-part-from-plugin | |
*/ | |
define('PLUGIN_DIR_PATH','Your-plugin-directory-path'); | |
function ccm_get_template_part($slug, $name = null) { | |
do_action("ccm_get_template_part_{$slug}", $slug, $name); | |
$templates = array(); | |
if (isset($name)) | |
$templates[] = "{$slug}-{$name}.php"; | |
$templates[] = "{$slug}.php"; | |
ccm_get_template_path($templates, true, false); | |
} | |
/* Extend locate_template from WP Core | |
* Define a location of your plugin file dir to a constant in this case = PLUGIN_DIR_PATH | |
* Note: PLUGIN_DIR_PATH - can be any folder/subdirectory within your plugin files | |
*/ | |
function ccm_get_template_path($template_names, $load = false, $require_once = true ) { | |
$located = ''; | |
foreach ( (array) $template_names as $template_name ) { | |
if ( !$template_name ) | |
continue; | |
/* search file within the PLUGIN_DIR_PATH only */ | |
if ( file_exists(PLUGIN_DIR_PATH . $template_name)) { | |
$located = PLUGIN_DIR_PATH . $template_name; | |
break; | |
} | |
} | |
if ( $load && '' != $located ) | |
load_template( $located, $require_once ); | |
return $located; | |
} | |
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 | |
/** | |
* you can call in any wordpress pages or post or else | |
*/ | |
ccm_get_template_part('content','custom-page'); | |
// or in directory | |
ccm_get_template_part('folder/content','custom-page'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment