WordPress插件:使用include而不是重定向

时间:2016-07-05 19:57:35

标签: php wordpress plugins

我正在开发一个WP插件,在启用维护时重定向用户;

if( ! is_page( intval( $maintenance_options['page'] ) ) ) {
    wp_redirect( get_permalink( $maintenance_options['page'] ) ); exit;
}

我不是重定向用户,而是include页面。这个例子是完全错误的,但只是为了清除它:

include($maintenance_options['page']);

...所以用户停留在索引而不是斜杠页面。

这可能,是否安全,我该如何执行?

1 个答案:

答案 0 :(得分:0)

action hook for this purpose名为template_include

  

此过滤器挂钩在WordPress包含预定模板文件之前立即执行。这可以用于覆盖WordPress的默认模板行为。

一个例子:

add_filter( 'template_include', 'maintenance_template', 99 );
function maintenance_template( $template ) {
    if( ! is_page( intval( $maintenance_options['page'] ) ) ) {
        $new_template = locate_template( array( 'path-to-template-relative-to-theme-root.php' ) );
        if ( '' != $new_template ) {
            return $new_template ;
        }
    }
    return $template;
}
相关问题