I am using the following code to display the logged in users unread message count in a template.
<?php if (messages_get_unread_count() != 0) { ?>
<div id="message_count">
<div class="countspan">
<?php echo messages_get_unread_count(); ?>
</div>
</div>
<?php } ?>
This works great however it requires a page refresh to update. Since I am making heavy use of ajax driven navigation throughout the site and the custom apps within it this is not a satisfactory solution.
How can make the counter update automatically?
I have played around with the 'lvive notifications' plugin which polls the srver every 10 seconds to provide live notifications but of course this does not interact with my custom unread message counter.
Any ideas guys? I could really use the help.
I've worked it out...
It turns out buddypress has built in ajax functions for a lot of this stuff...
So for anyone else wanting to do the same thing.... It is a simple case of putting the 'count' inside an a link with the following id.
<a id="user-messages">
<span><?php echo messages_get_unread_count(); ?></span>
</a>
Buddypress' javascript then does the rest for you. Simple!
Mana thanks for your suggestion though.
You could use setTimeout to periodically fire a request to the server which invokes your messages_get_unread_count()
and returns a value.
Then based on the value returned, you could show or hide your <div id="message_count">
with the updated count.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With