有没有办法用插件覆盖 WordPress 模板? [英] Is there any way to override a WordPress template with a plugin?

查看:43
本文介绍了有没有办法用插件覆盖 WordPress 模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个登陆页面.如果插件检测到一些 GET 或 POST 请求,它应该覆盖 wordpress 主题并显示自己的主题.

I'd like to make a landing page. If plugin detects some GET or POST requests it should override wordpress theme and show its own.

它会以某种方式工作:

if (isset($_GET['action']) && $_GET['action'] == 'myPluginAction'){
    /* do something to maintain action */
    /* forbid template to display and show plugin's landing page*/
}

我熟悉 WP Codex,但我不记得是否有任何功能可以做到这一点.当然,我用谷歌搜索没有结果.

I'm familiar with WP Codex, but I don't remember if there is any function to do that. Of course, I googled it with no results.

提前感谢您的任何想法.

Thanks for any ideas in advance.

推荐答案

你需要钩子 template_include.Codex 中似乎没有记录,但您可以在 SO 或 WordPress StackExchange<中找到更多示例/p>

插件文件

<?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-world/?mod=yes.

这篇关于有没有办法用插件覆盖 WordPress 模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆