我想制作一个登陆页面。如果插件检测到某些 GET 或 POST 请求,它应该覆盖 WordPress 主题并显示自己的主题。
它会以某种方式工作:
if (isset($_GET['action']) && $_GET['action'] == 'myPluginAction'){
/* do something to maintain action */
/* forbid template to display and show plugin's landing page*/
}
我很熟悉WP Codex,但我不记得是否有任何函数可以做到这一点。当然,我用谷歌搜索没有结果。
感谢您提前提供任何想法。
最佳答案
你需要钩子(Hook)template_include
。它似乎没有记录在 Codex 中,但您可以在 SO 或 WordPress StackExchange 中找到更多示例。
插件文件
<?php
/**
* Plugin Name: Landing Page Custom Template
*/
add_filter( 'template_include', 'so_13997743_custom_template' );
function so_13997743_custom_template( $template )
{
if( isset( $_GET['mod']) && 'yes' == $_GET['mod'] )
$template = plugin_dir_path( __FILE__ ) . 'my-custom-page.php';
return $template;
}
插件文件夹中的自定义模板
<?php
/**
* Custom Plugin Template
* File: my-custom-page.php
*
*/
echo get_bloginfo('name');
结果
使用 ?mod=yes
访问网站的任何 url 将呈现插件模板文件,例如:http://example.com/hello-世界/?mod=yes
.
关于wordpress - 有没有办法用插件覆盖 WordPress 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13997743/