$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Wordpress member notification|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Wordpress member notification

matteradmin9PV0评论

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
  • Where does 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
Add a comment  | 

2 Answers 2

Reset to default 2

PHP 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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far