我可以添加“虚拟"吗?WordPress 中的页面? [英] Can I add "virtual" page in WordPress?

查看:26
本文介绍了我可以添加“虚拟"吗?WordPress 中的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 WordPress 网站上,我想创建虚拟"页面.虚拟页面是指我可以通过 URL 链接到的内容.我正在使用 Facebook Webhooks,我想要一个这样的回调 URL:https://example.com/facebook-webhook.我希望它通过插件来完成,而不实际向数据库添加任何页面.有没有办法在 WordPress 中以这种方式添加页面?

On my WordPress site, I would like to create "virtual" page. By virtual page I mean the content that I can link to by URL. I'm using Facebook Webhooks and I would like to have a callback URL like this: https://example.com/facebook-webhook. I'd like it to be done via plugin, without actually adding any page to the database. Is there any way to add a page in WordPress that way?

现在我正在使用 https://example.com/?facebook-webhook 并检查 isset( $_GET['facebook-webhook'] )init 操作中.不过,我希望它没有 ?.

For now I'm using https://example.com/?facebook-webhook and checking for isset( $_GET['facebook-webhook'] ) in the init action. I would love to have it without the ? though.

推荐答案

您可以使用重写和查询变量来执行此操作,以将您的自定义 php 文件用作模板:

You can do this using a rewrite and query vars to use your custom php file as a template:

//1. define a path for later
define('PLUG_PATH', WP_PLUGIN_DIR . '/' . basename(dirname(__FILE__)));



//2. add a wp query variable to redirect to
add_action('query_vars','plugin_set_query_var');
function plugin_set_query_var($vars) {
    array_push($vars, 'is_new_page'); // ref url redirected to in add rewrite rule

    return $vars;
}


//3. Create a redirect
add_action('init', 'plugin_add_rewrite_rule');
function plugin_add_rewrite_rule(){
    add_rewrite_rule('^mynewpage$','index.php?is_new_page=1','top');

    //flush the rewrite rules, should be in a plugin activation hook, i.e only run once...

    flush_rewrite_rules();  
}

//4.return the file we want...
add_filter('template_include', 'plugin_include_template');
function plugin_include_template($template){


    // see above 2 functions..
    if(get_query_var('is_new_page')){
        //path to your template file
        $new_template = PLUG_PATH.'/template.php';
        if(file_exists($new_template)){
            $template = $new_template;
        } 
        // else needed? up to you maybe 404?
    }    

    return $template;    

}

这篇关于我可以添加“虚拟"吗?WordPress 中的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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