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

时间:2016-02-02 19:07:43

标签: php wordpress wordpress-plugin

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

目前我正在使用https://example.com/?facebook-webhook并在isset( $_GET['facebook-webhook'] )操作中检查init。我很乐意在没有?的情况下拥有它。

1 个答案:

答案 0 :(得分:1)

您可以使用重写和查询变量将此自定义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;    

}
相关问题