繁体   English   中英

我可以在WordPress中添加“虚拟”页面吗?

[英]Can I add “virtual” page in WordPress?

在我的WordPress网站上,我想创建“虚拟”页面。 虚拟页面是指我可以通过URL链接到的内容。 我正在使用Facebook Webhooks,并且我想要一个这样的回调URL: https://example.com/facebook-webhook ://example.com/facebook-webhook。 我希望可以通过插件完成操作,而无需将任何页面实际添加到数据库中。 有什么办法可以在WordPress中添加页面吗?

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

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

//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;    

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM