Show Number of Notifications
-
Hello,
I want to show forum notifications in WordPress. However, it is not possible to do as in phpBB. So I thought of doing it like this: https://i.hizliresim.com/bd3ax3f.jpgI will do the same for mobile. I will add it to the dropdown menu icon and the forum link when the menu opens.
For a private use when I’m in the forum I can display it like this:
<!-- IF NOTIFICATIONS_COUNT > 0 --><span class="blablax <!-- IF NOTIFICATIONS_COUNT > 9 -->blablaxxl<!-- ENDIF --> <!-- IF NOTIFICATIONS_COUNT > 99 -->blablaxxxl<!-- ENDIF -->">{NOTIFICATIONS_COUNT}</span><!-- ENDIF -->
In short the following code is required:
<!-- IF NOTIFICATIONS_COUNT > 0 -->{NOTIFICATIONS_COUNT}<!-- ENDIF -->
So how can I show these notification numbers in WordPress?
Thanks.
-
What do you recommend? I use the following code to make it appear next to the menu item:
function add_notification_count_to_menu($item_output, $item, $depth, $args) { if ($args->theme_location == 'primary' && $item->title == '<i class="fas fa-comment"></i> Forumlar') { global $w3all_phpbb_unotifications; $item_output .= '<span class="bildirimvarbaksana">'. count($w3all_phpbb_unotifications) .'</span>'; } return $item_output; } add_filter('walker_nav_menu_start_el', 'add_notification_count_to_menu', 10, 4);
Then I removed the JS code you gave in my last posts and saw that nothing actually changed. The only thing that changed was the plugin’s files.
Now, I reset the plugin files. So I downloaded the new one from the WordPress server and restored the files we changed. Then I just made the changes you originally mentioned in the class.wp.w3all-phpbb.php file. The good news is it works. It shows the number of notifications correctly.
It either broke down or stopped working right after.
Then I edited the other file.
Then I edited the other one as well. Then he went back to his old self.
Not working. We’re skipping something.
Okay, if there are 0 notifications and it shows the number 1, then if there is at least 1 notification, it will appear. I tried to write code like this:
global $w3all_phpbb_unotifications; if (count($w3all_phpbb_unotifications) > 1){ $item_output .= '<span class="bildirimvarbaksana">'. count($w3all_phpbb_unotifications) .'</span>'; }
If count is greater than 0, it will show 1 even if there is no notification. If it is greater than 1, it will not show when there is 1 notification, but when there are 2 or more. This shows that there is no error in the php code I use. Like I said, we’re missing something.
I would like to add so that there is no missing information. In the previous code, I prevented the continuous display of 1 number. It now shows if there are 2 notifications. Because before that it was always showing 1.
By the way, this code is not valid for Admin. Because their accounts are not matched. But it works for other users.
I would still like the number 1 to be shown if there is at least one notification though. so I could change the code to if count > 0.
You understand how important what we’re doing can be. Showing notifications only if there are more than 1 notifications can cause negativity.
Kind regards.
Hey! Yes, if not wrong, i suggest to do this maybe:
global $w3all_phpbb_unotifications; if (!empty($w3all_phpbb_unotifications) && count($w3all_phpbb_unotifications) > 0){ $item_output .= '<span class="bildirimvarbaksana">'. count($w3all_phpbb_unotifications) .'</span>'; }
Or
global $w3all_phpbb_unotifications; if (!empty($w3all_phpbb_unotifications) && count($w3all_phpbb_unotifications) > 0){ $item_output .= '<span class="bildirimvarbaksana">'. count($w3all_phpbb_unotifications) .'</span>'; } else { // code that run if no notifications }
[CODE EDITED]
- This reply was modified 2 years, 2 months ago by axew3.
note that the code above has been edited, you have to check that the array is not empty, before to count(). The snippet is correct int his way and will never fail
- This reply was modified 2 years, 2 months ago by axew3.
Perfect. It works. ?? Thank you so much.
By the way, the JS code you provided is also active.
One more thing occurred to me. But I’m not sure if it’s necessary.
As in phpBB, if there is a notification it will appear in the tab:
How can we do this in WordPress? ??
- This reply was modified 2 years, 2 months ago by Halil ESEN.
Hello again,
I ran into a problem in 2.6.6. I was doing one of my routine checkups. Then I noticed that the notification counts no longer appear. I have all the JS code we made here. The plugin’s phpBB user notifications setting was “Yes, but exclude Private Messages”. After much digging I partially found where the problem lies. Because when I set the setting to “Yes, also Private Messages”, it only showed the number of notifications for PM. So the extension does not show the normal number of notifications.I checked from incognito tab and another browser. Also, cache is disabled for logged in users.
How can we solve this?
So, let show how is possible to display everything with an example.
Let assume we are on theme twentysixteen so we go to open (but it could be any other file, and the same can be done into any template or plugin function (assuming that the code that generate data into the integration plugin, hook into an init action):
/wp-content/themes/twentysixteen/header.phpexactly after the starting
<?php
tag we go to add this:global $w3all_phpbb_unotifications; echo'<pre>'; print_r($w3all_phpbb_unotifications); exit;
reload WP on browser.
You’ll see that if there are notifications, the array of all notifications (included pm messages if there are and the option is set to yes) will show.
You could use count() to check the number of all new notificationsglobal $w3all_phpbb_unotifications; if(!empty($w3all_phpbb_unotifications)){ echo count($w3all_phpbb_unotifications); } else { echo 'there are not new notifications'; } exit;
or to get only PMs count, you’ll have to iterate into array, and get number of notifications, which are notification_type_id value 11 (new PMs).
But to get ONLY the number of news phpBB PMs and check if there are, you can also use the
$w3all_phpbb_usession
easier way instead:global $w3all_phpbb_usession; print_r($w3all_phpbb_usession); exit;
this array (as you’ll see) contain already these values (and all about the phpBB user, so remeber this that is very important), as example:
[user_new_privmsg] => 2 [user_unread_privmsg] => 4
so that to display each of these values can be done just like this:
global $w3all_phpbb_usession; echo $w3all_phpbb_usession->user_new_privmsg; // display num of news pms echo $w3all_phpbb_usession->user_unread_privmsg; // display num of all unread
these values are coming with 0 if there are NOT new unread or new PMs
I just want to show the notification count. I have the following code to show up next to the forums link:
function add_notification_count_to_menu($item_output, $item, $depth, $args) { if ($args->theme_location == 'primary' && $item->title == '<i class="fas fa-comment"></i> Forumlar') { global $w3all_phpbb_unotifications; if (!empty($w3all_phpbb_unotifications) && count($w3all_phpbb_unotifications) > 0){ $item_output .= '<span class="bildirimvarbaksana">'. count($w3all_phpbb_unotifications) .'</span>'; } } return $item_output; } add_filter('walker_nav_menu_start_el', 'add_notification_count_to_menu', 10, 4);
There’s also this one, but I don’t know what it does:
function phpbb_notifications() { global $w3all_phpbb_unotifications; if(!empty($w3all_phpbb_unotifications)){ //print_r(count($w3all_phpbb_unotifications));exit; echo'<script>var w3notificationsCount = '.count($w3all_phpbb_unotifications).';</script>'; } else { echo'<script>var w3notificationsCount = 0;</script>'; // or remmove and check in javascript after, if the var exist, to detect that the user have or do not have new notifications } } add_action( 'wp_head', 'phpbb_notifications', 0 );
The option in the settings is marked “Yes, but exclude Private Messages”. However, it still only shows the PM number. I just want to get, in general, the number of notifications that appear in the notification bell.
We achieved this with the edits we made before updating the plugin. But after updating it is gone.
what do you think?
After restoring version 2.6.5 and making the changes mentioned in this topic, it worked as it should. I will continue to use 2.6.5 until the next release. Maybe forever. ??
By the way, don’t get me wrong, you have a bad habit of being quiet when you give up on something. For example, not replying to this topic at all. There is also a topic here. I’m sure you have good reasons, but it’s much better to at least give a positive or negative answer.
Hello!
Unfortunately, i have several problems to be resolved in short, or my life will fall into a disaster! ??
I am sorry if sometime i miss something along the way, anyway the essential is granted, while additions are applied when possible due to some other task i have to accomplish to.There are no changes into 2.6.6 except about notifications addition and few fixes more. Strange that you have not find out the way to apply the same above into 2.6.6!
If you want to provide the files that you are using to reproduce things, i will be glad to show you how to fix
I hope you can work everything out; I understand.
When I looked at the files again, I saw that in 2.6.6 there was no code:
if(!empty($phpbb_user_session[0]->notification_data)){ // there is almost an unread notification for this user // so we go to retrieve all unreads for this user session $w3all_phpbb_unotifications = $w3all_phpbb_connection->get_results("SELECT * FROM ". $w3all_config["table_prefix"] ."notifications WHERE ". $w3all_config["table_prefix"] ."notifications.user_id = ".$phpbb_user_session[0]->user_id." AND ". $w3all_config["table_prefix"] ."notifications.notification_read = 0",ARRAY_A); }
- The topic ‘Show Number of Notifications’ is closed to new replies.