首页 > 解决方案 > WP Woo - 每次调用链接时检查端点是否存在

问题描述

更新:

没有人要求否决!反对我的解决方案,所以我假设以这种方式工作是可以的。


问题/解决方案:

这是指我之前的问题:

WP Woo - 保存 Woo 设置后删除自定义端点

我没有看到任何其他解决方案,但每次调用链接时都强制检查端点是否存在。

我想知道:这种方法的缺点是什么?

我最终得到的代码(经过多次测试,似乎工作正常):

add_action('template_redirect', array($this, 'check_the_endpoint'));

function check_the_endpoint() {
    global $wp;
    if ( ! is_admin() ) {
        if( ! empty($wp->query_vars['attachment']) && $wp->query_vars['attachment'] == 'moje-punkty') {
            echo 'moje-punkty - 404 error';

            add_rewrite_endpoint( 'moje-punkty', EP_ROOT | EP_PAGES );
            $this->foc_points_flush_rewrite_rules();

            header( 'Location: ' . home_url( $wp->request ));
        }
        else if( isset($wp->query_vars['moje-punkty'])) {
            echo 'moje-punkty - works!';
        }
        else {
            echo 'any other page';
        }
    }
}

标签: wordpresswoocommerceendpoint

解决方案


如果有人遇到同样的问题,这似乎是一个答案:

add_action('template_redirect', array($this, 'check_the_endpoint'));

function check_the_endpoint() {
    global $wp;
    if ( ! is_admin() ) {
        if( ! empty($wp->query_vars['attachment']) && $wp->query_vars['attachment'] == 'moje-punkty') {
            echo 'moje-punkty - 404 error';

            add_rewrite_endpoint( 'moje-punkty', EP_ROOT | EP_PAGES );
            $this->foc_points_flush_rewrite_rules();

            header( 'Location: ' . home_url( $wp->request ));
        }
        else if( isset($wp->query_vars['moje-punkty'])) {
            echo 'moje-punkty - works!';
        }
        else {
            echo 'any other page';
        }
    }
}

其中,很明显,每个ECHOELSE IFELSE都将被删除 - 这只是为了测试目的而添加的。


推荐阅读