I try to implement a function to notify users about changes on the webpage. I tried to use sessions for that but it did not work very well. For example:
A User can attend an event. To do that he has to register for it. So the status of his registration is 'on hold' at that moment. In the backend, i can accept his registration and change the status to 'accepted'. Now I want the user to see a notification on his dashboard that he was accepted.
For this, I use his last login date. I check if the change_date of his status was after his last login date. The value for registration_notifications is stored in a wp_session variable. This works without problem. If the user opens the page with the list of his registrations, this wp_session variable is reseted. And here comes the problem:
If I now change the status of a second registration of the same user, the notification is not shown.
My codes:
the function to get the notifications:
if(!isset(wp_session['event'])){
wp_session['event']= $wpdb->get_var(SELECT COUNT..... change_date > last_login_date);
}
show notification:
if(wp_session['event']!=-1){ echo wp_session['event']}
change session on the overview page:
wp_session['event']=-1;
I need to change the code so, that I can recognize if there was a change in the amount of the notifications while the user was already online. But I could not figure out how to do that the best way.
Thanks in advance.
I try to implement a function to notify users about changes on the webpage. I tried to use sessions for that but it did not work very well. For example:
A User can attend an event. To do that he has to register for it. So the status of his registration is 'on hold' at that moment. In the backend, i can accept his registration and change the status to 'accepted'. Now I want the user to see a notification on his dashboard that he was accepted.
For this, I use his last login date. I check if the change_date of his status was after his last login date. The value for registration_notifications is stored in a wp_session variable. This works without problem. If the user opens the page with the list of his registrations, this wp_session variable is reseted. And here comes the problem:
If I now change the status of a second registration of the same user, the notification is not shown.
My codes:
the function to get the notifications:
if(!isset(wp_session['event'])){
wp_session['event']= $wpdb->get_var(SELECT COUNT..... change_date > last_login_date);
}
show notification:
if(wp_session['event']!=-1){ echo wp_session['event']}
change session on the overview page:
wp_session['event']=-1;
I need to change the code so, that I can recognize if there was a change in the amount of the notifications while the user was already online. But I could not figure out how to do that the best way.
Thanks in advance.
Share Improve this question asked Feb 15, 2019 at 18:43 goragora 1 1 |2 Answers
Reset to default 2PHP only runs when you request a page. When the request is over, everything is gone. It's not like a Node application where the same program handles all the requests and variables can persist across pages just by defining them, so doing things with a $wp_session
variable will only do things on that request.
If you navigate to another page, it has to load WordPress again from scratch, and figure everything out fresh. This is why your code doesn't work, and that's how all PHP programs work.
If you want to persist something, you have to store it somewhere, such as the database, the filesystem, or an object cache. That's what things like user meta for logged in users are used for.
There is 1 exception, PHP Sessions using $_SESSION
variables, but, they're not used by WordPress and need some setup. Additionally, they're incompatible with page caching solutions such as Varnish, Supercache, etc. Additionally, not all hosts support it. For example, PHP Sessions will not work correctly on WP Engine.
Other notes:
- In PHP variables begin with
$
but yours don't for some reason. I would have expected PHP fatal errors. - Global variables need to be declared before they're used
- You're doing an SQL query then dumping the result in a variable with no error processing which is a security risk
- You're displaying the result of that query without any formatting or escaping, which is also a security risk
- You need to end each statement in a
;
, your show notification example doesn't do this, and will generate syntax errors wp_session
is never created anywhere, so initially it's completely undefined. This will generate warnings and notices at runtime that fill your PHP error log
Are the registrations saved as custom posts in the database?
If so, perhaps you could have a private taxonomy / term call "not_seen". Whenever there are changes on the registration you would attach the custom term to it.
Then add ajax to the page with the list of registrations. When the user visits the page the ajax kicks in and unsets the "not_seen" term from the registrations.
wp_session
come from? WP doesn't use PHP sessions and a number of hosts won't work with it. Additionally it won't be possible to cache pages with PHP sessions – Tom J Nowell ♦ Commented Feb 15, 2019 at 19:16