wordpress - 有没有办法用插件覆盖 WordPress 模板?

标签 wordpress

我想制作一个登陆页面。如果插件检测到某些 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/

相关文章:

WordPress wp_title 索引页上为空白

html - 列中不同的背景颜色,同一页

php - Gravity Forms post/get 从 mysql 数据库获取数据

html - 如何在我的网站上居中对齐此表单

php - 扩展 WordPress 用户查询

html - 与博客内容一起运行的粘性横幅

css - 顶部栏菜单在移动 View 中不可见

jquery - WordPress 中的自定义 ajax 请求

WordPress:仅当使用小部件时才将脚本排入队列

javascript - 如何使用 Jquery 更改图像大小并更改下一张/上一张图像?