Detect if requested page is PWA on server side


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 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));
add_action('template_redirect', 'addIsPwaQueryArg');

Can someone tell me what’s wrong with my code and why is it not working?


DaftPlug 2 years 2021-03-29T16:05:38-05:00 0 Answers 0 views 0

Leave an answer