来自插件的模板重载

来自插件的模板重载

Codex Home → BuddyPress Plugin Development → Template Overload from a Plugin
Template Overload from a Plugin

Note: overloading and / or adding templates from plugins can be quite complex. For more detailed discussion and examples, please see:

Upgrading older plugins that bundle custom templates for BP 1.7
Using BP Theme Compat in Plugins
Theme Compatibility & Template Files

The purpose of this page is to provide a simple example of overloading an existing template part. The example shown will allow you to overload the member-header.php file which creates the BuddyPress profile page header section (avatar, messaging buttons, profile navigation menu etc.) for profile pages where cover images are not used. If you are using cover images on the users profile then the file to overload is cover-image-header.php. Both of these files are located in plugins/buddypress/bp-templates/bp-legacy/members/single.
If you』re using BP 1.7 or greater and not using the bp-default theme, you can use this approach…
It consists of 2 files and a template folder structure:

loader.php
bp-tol.php
templates/members/single/member-header-tol.php

loader.php
/*
Plugin Name: BP Template Overload
Plugin URI: http://philopress.com
Description: Load templates from a plugin
Version: 1.0
Author: shanebp
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/

if ( !defined( 'ABSPATH' ) ) exit;

function bp_tol_init() {

define( 'BP_TOL_DIR', dirname( __FILE__ ) );

require( BP_TOL_DIR . '/bp-tol.php' );

}
add_action( 'bp_include', 'bp_tol_init' );

bp-tol.php
// register the location of the plugin templates
function bp_tol_register_template_location() {
return BP_TOL_DIR . '/templates/';
}

// replace member-header.php with the template overload from the plugin
function bp_tol_maybe_replace_template( $templates, $slug, $name ) {

if( 'members/single/member-header' != $slug )
return $templates;

return array( 'members/single/member-header-tol.php' );
}

function bp_tol_start() {

if( function_exists( 'bp_register_template_stack' ) )
bp_register_template_stack( 'bp_tol_register_template_location' );

// if viewing a member page, overload the template
if ( bp_is_user() )
add_filter( 'bp_get_template_part', 'bp_tol_maybe_replace_template', 10, 3 );

}
add_action( 'bp_init', 'bp_tol_start' );

member-header-tol.php
Make a copy of
buddypress/bp-templates/bp-legacy/buddypress/members/single/member-header.php
rename it and make some small change so that it』s obvious that it』s being loaded.
Then save it in the folder structure shown above.

That』s it !
And you can overload member-header-tol.php by placing a copy in your child-theme directory: [theme]/buddypress/members/single/member-header-tol.php

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注