如何使用插件自定义模板覆盖WordPress标题

如何使用插件自定义模板覆盖WordPress标题,wordpress,plugins,Wordpress,Plugins,我正在尝试使用插件显示自定义页面。它仅在URL匹配时显示。问题是,该页面通常为404,因为不存在具有该地址的页面或帖子。因此,它显示404页标题未找到的页面 这就是我正在做的 add_filter('template_include', [$this, 'pageTeamplate']); public function pageTeamplate($templates, $content=''){ global $wp; if($wp->query_vars['page

我正在尝试使用插件显示自定义页面。它仅在URL匹配时显示。问题是,该页面通常为404,因为不存在具有该地址的页面或帖子。因此,它显示404页标题
未找到的页面

这就是我正在做的

add_filter('template_include', [$this, 'pageTeamplate']);

public function pageTeamplate($templates, $content=''){
    global $wp;
    if($wp->query_vars['pagename'] != 'forehand-news')
        return $templates;

    get_header();
    wp_enqueue_style('baseplugin-frontend');
    wp_enqueue_script('baseplugin-frontend');
    $response = wp_remote_request("http://backoffice.localhost/app/news",
                array(
                    'method'     => 'GET'
                )
            );

    //var_dump($response);
    //echo  $response;
    $divData = wp_remote_retrieve_body($response);
    echo "<script>window.news = $divData </script>";
    echo '<div id="vue-frontend-app"></div>';
    get_footer();
}
add_filter('template_include',[$this,'pageTeamplate']);
公共函数pageTeamplate($templates,$content=''){
全球$wp;
如果($wp->query_vars['pagename']!='forehand news')
返回$模板;
获取_头();
wp_排队_样式(“baseplugin-frontend”);
wp_排队_脚本('baseplugin-frontend');
$response=wp\u远程请求(“http://backoffice.localhost/app/news",
排列(
'方法'=>'获取'
)
);
//var_dump($response);
//回音$应答;
$divData=wp\u remote\u retrieve\u body($response);
echo“window.news=$divData”;
回声';
获取页脚();
}
页面将显示其内容,但标题仍为
page not found


如何更改标题或根据URL显示自定义插件页面的最佳方式?

将以下代码片段添加到活动主题的functions.php文件中:


问题是我正在开发一个插件,它可以被任何人使用。所以我不能编辑任何主题的函数。那么,做这些事情的最佳方式是什么呢?@ShakilAhmed您也可以在自定义插件中添加相同的内容。我只是为了测试目的添加了这一行。这样它就可以工作了,而且
pre\u get\u document\u title
这个过滤器也可以工作。您能告诉我使用
template\u include
ok添加自定义模板吗?或者有其他最好的方法吗?谢谢,但当主题更新时,它将被覆盖(?)@peter最好将您的所有自定义添加到您的子主题中,这不会影响主题更新
function modify_404_page_title( $title_parts ) {
    if ( is_404() ) {
        $title_parts['title'] = 'ADD 404 TITLE TEXT HERE';
    }
    return $title_parts;
}
add_filter( 'document_title_parts', 'modify_404_page_title' );