我没有完全测试下面描述的方法,也没有测试正常的“孙子”主题,但给出了template_include 过滤器:
/*
Plugin Name: Grandchild Themes
Plugin URI: http://www.who-cares.com/
Description: A concept for Grandchild themes Plugin
Author: See Plugin URI
Version: 0.0.0.0.1-Alpha
*/
add_action(\'wp_head\', \'grnd_chld_add_headers\', 0);
add_action(\'init\', \'grnd_chld_add_css\');
// Load template if exists.
function grandchild_template_include( $template ) {
if ( file_exists( untrailingslashit( plugin_dir_path( __FILE__ ) ) . \'/grnd_chld_templates/\' . basename( $template ) ) )
$template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . \'/grnd_chld_templates/\' . basename( $template );
return $template;
}
// This is the actual filter that we want .
add_filter( \'template_include\', \'grandchild_template_include\', 11 );
function grnd_chld_add_headers () {
wp_enqueue_style(\'my_grandchild_style\');
}
function grnd_chld_add_css() {
$stamp = @filemtime(plugin_dir_path(__FILE__).\'/style.css\'); // easy versioning
wp_register_style (\'my_grandchild_style\', plugins_url(\'style.css\', __FILE__).\'\', array(), $stamp);
}
// From here , what you got is like a normal functions.php.
您还可以以类似的方式尝试更具体的过滤器,例如
archive_templateadd_filter (\'archive_template\', create_function (\'\', \'return plugin_dir_path(__FILE__)."archive.php";\'));
尽管如此,我不确定这是做事情的最佳方式。