admin管理员组文章数量:1297093
I'm trying to determine if the user is browsing PWA on server side. On client side I can check if the browser mode is standalone
via JavaScript and detect PWA, but on server side this is not an option. So I'm using simple query parameter for that. Start URL in manifest has query param isPwa
appended and every time user opens PWA, it goes to /?isPwa
. What I need now is to keep that parameter while user browses PWA, so I need to set isPwa
parameter to next URL that user opens if the referer URL already had isPwa
parameter, but my code goes to redirection loop and I'm unable to identify the cause of this. Here's my code:
function addIsPwaQueryArg() {
$referer = wp_get_referer();
if (strpos($referer, 'isPwa') !== false) {
$location = remove_query_arg('isPwa');
wp_redirect(add_query_arg('isPwa', '', $location));
exit;
}
}
add_action('template_redirect', 'addIsPwaQueryArg');
Can someone tell me what's wrong with my code and why is it not working?
Thanks!
I'm trying to determine if the user is browsing PWA on server side. On client side I can check if the browser mode is standalone
via JavaScript and detect PWA, but on server side this is not an option. So I'm using simple query parameter for that. Start URL in manifest has query param isPwa
appended and every time user opens PWA, it goes to https://example/?isPwa
. What I need now is to keep that parameter while user browses PWA, so I need to set isPwa
parameter to next URL that user opens if the referer URL already had isPwa
parameter, but my code goes to redirection loop and I'm unable to identify the cause of this. Here's my code:
function addIsPwaQueryArg() {
$referer = wp_get_referer();
if (strpos($referer, 'isPwa') !== false) {
$location = remove_query_arg('isPwa');
wp_redirect(add_query_arg('isPwa', '', $location));
exit;
}
}
add_action('template_redirect', 'addIsPwaQueryArg');
Can someone tell me what's wrong with my code and why is it not working?
Thanks!
Share Improve this question asked Mar 29, 2021 at 21:05 DaftPlugDaftPlug 31 silver badge4 bronze badges 1- 1 You shouldn't rely on the referrer, it can be modified by the user, spoofed, and lots of browsers and extensions strip it for privacy purposes – Tom J Nowell ♦ Commented Mar 29, 2021 at 21:40
1 Answer
Reset to default 0You're not passing the actual querystring to remove_query_arg()
. After some quick testing, I think your $location
value is probably an empty string, and the location you're attempting to redirect to is ?isPwa
.
The 2nd parameter to remove_query_arg()
should be the $referer
value.
function addIsPwaQueryArg() {
$referer = wp_get_referer();
if (strpos($referer, 'isPwa') !== false) {
$location = remove_query_arg('isPwa', $referer );
wp_redirect(add_query_arg('isPwa', '', $location));
exit;
}
}
add_action('template_redirect', 'addIsPwaQueryArg');
However: I'm not sure that's the root cause of your redirect loop. It looks like you're checking to see if isPwa
is in the querystring, removing it if it is, and then adding it back in (with a blanked value) as you redirect. Is this your intent?
本文标签: plugin developmentDetect if requested page is PWA on server side
版权声明:本文标题:plugin development - Detect if requested page is PWA on server side 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741648460a2390329.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论