79100927

Date: 2024-10-18 07:09:05
Score: 0.5
Natty:
Report link

I think that you can achieve this by altering your code a little bit.

function add_login_check()
{
if(!isset($_SESSION['didit'])) {
  if ( is_user_logged_in() && is_page() ) {
    ?>

    <script>window.location='/user-area/'</script>

    <?php
    exit();
    $_SESSION['didit'] = true; //Esssentially your session declaration here is unreachable , because there is a redirection and a `exit()` before it.
  }

}
}
add_action('wp', 'add_login_check');

Please try the code like this .

function add_login_check()
{
if(!isset($_COOKIE['didit'])) {
  if ( is_user_logged_in() && is_home() ) {

setcookie("didit", true, 0, "/"); // Setted a cookie with the name of "didit" and the value of "true" , with lifetime of 0 ( meaning that when the browser closes the cookie will be expired

    ?>

    <script>window.location='/user-area/'</script>

    <?php
    exit();
  }

}
}
add_action('wp', 'add_login_check');

You can also use is_home() or is_front_page() instead of is_page(page_id) that you are currently using as far as you intent to use this code

https://developer.wordpress.org/reference/functions/is_home/ https://developer.wordpress.org/reference/functions/is_front_page/

I also believe that cookie is a better practice than session variable , because you can alter the cookie life span , from 0 ( browser closes means that cookie is expires from x amount of time).

https://www.php.net/manual/en/reserved.variables.cookies.php

The current code and changes are untested , so please if you test them you can give feedback and i can assist you further. Cheers!

Reasons:
  • Blacklisted phrase (1): Cheers
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: cstudio