替换插件中的get_header模板



我正在创建一个插件,它覆盖插件文件夹中包含的模板。首先,我试图覆盖header.php和footer.php文件。尝试创建各种解决方案,但其中任何一个都不起作用。(这是插件解决方案之一(
尝试删除get_header挂钩并添加类似的新内容,但这没有帮助。

有一些最新的解决方案,我尝试了

function header_override () {
// $template = locate_template('header.php');
// load_template(plugin_dir_path( __FILE__ ) . 'templates/header.php');
// print_r($template);
if ( $overridden_template = locate_template( 'header.php' ) ) {
load_template( $overridden_template );
}else{
load_template(plugin_dir_path( __FILE__ ) . 'templates/header.php');
}
}
add_action('template_include', 'header_override');

function new_header_output() {
remove_action( 'get_header', 'header_output', 20);
load_template(plugin_dir_path( __FILE__ ) . 'templates/header.php');
}
add_action('get_header', 'new_header_output');

function action_function_name_391( $name, $args ){
print_r("custom header");
}
add_action( 'get_header', 'action_function_name_391', 10, 2 );

也许有人有类似的问题来覆盖主题模板

add_action('get_header', 'replace_header');
function replace_header(){
require PLUGIN_DIR.'includes/templates/header.php';
$templates   = [];
$templates[] = 'header.php';
remove_all_actions( 'wp_head' );
ob_start();
locate_template( $templates, true );
ob_get_clean();

}

最新更新