Code Snippets: Storing plugin theme files in the plugin dir (1.2)
-
This was quite an adventure figuring out how to make this work, so I wanted to share my method. Thanks to Andy for making a couple of bug fixes in TRUNK so this will work properly. You must be using revision 2525 or higher!
Also thanks to DJPaul for pointing me in the right direction in the beginning.
First you need a custom locate function that checks the WP stylesheet path first, and then checks your custom theme dir under your plugin dir. If you need to fall back on bp-default templates for some reason, then you will need to check the WP template path as well.
You can also use this in your templates to load sub-templates.
/**
* Check if template exists in style path, then check custom plugin location
*
* @param array $template_names
* @param boolean $load Auto load template if set to true
* @return string
*/
function bp_links_locate_template( $template_names, $load = false ) {
if ( !is_array( $template_names ) )
return '';
$located = '';
foreach($template_names as $template_name) {
// split template name at the slashes
$paths = split( '/', $template_name );
// only filter templates names that match our unique starting path
if ( !empty( $paths[0] ) && 'bp-links-default' == $paths[0] ) {
$style_path = STYLESHEETPATH . '/' . $template_name;
$plugin_path = BP_LINKS_PLUGIN_DIR . "/themes/{$template_name}";
if ( file_exists( $style_path )) {
$located = $style_path;
break;
} else if ( file_exists( $plugin_path ) ) {
$located = $plugin_path;
break;
}
}
}
if ($load && '' != $located)
load_template($located);
return $located;
}Now you need to filter the template locating results of bp_core_catch_uri.
/**
* Filter located BP template
*
* @see bp_core_load_template()
* @param string $located_template
* @param array $template_names
* @return string
*/
function bp_links_filter_template( $located_template, $template_names ) {
// template already located, skip
if ( !empty( $located_template ) )
return $located_template;
// only filter for our component
if ( $bp->current_component == $bp->links->slug ) {
return bp_links_locate_template( $template_names );
}
return '';
}
add_filter( 'bp_located_template', 'bp_links_filter_template', 10, 2 );I also created a wrapper around bp_core_load_template in case I need to filter something later.
/**
* Use this only inside of screen functions, etc
*
* @param string $template
*/
function bp_links_load_template( $template ) {
bp_core_load_template( $template );
}There you have it. Let me know if you find any bugs or make any improvements!
- The topic ‘Code Snippets: Storing plugin theme files in the plugin dir (1.2)’ is closed to new replies.