wordpress - WP 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';
}
}
}
解决方案
如果有人遇到同样的问题,这似乎是一个答案:
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';
}
}
}
其中,很明显,每个ECHO和ELSE IF和ELSE都将被删除 - 这只是为了测试目的而添加的。
推荐阅读
- angular - 如何从一个组件获取值或数组到另一个组件而不以角度 8 存储在本地存储中
- c# - 初始化要并行执行的任务列表(对于 Task.WhenAll)的正确方法是什么?
- python - Pygame不能在Y轴上射击
- asp.net-core - 与部署在 IIS 中的多个子域站点共享相同的会话(aspnet 核心)
- javascript - 如何将数组添加到单个推送数据中?
- r - tidyeval 取消引用用户创建的变量以供后续使用
- javascript - Js Uncaught (in promise) DOMException 录制视频
- r - 使用 ifelse 创建新变量
- python - 为什么我的 python 代码只加密大写的消息?
- django - 姜戈休息。图片 url 发送到 html 页面但不发送到图片位置