Home › Toolset Professional Support › [Resolved] Messaging module: Group chat and number of unread messages
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | - | - | 9:00 – 13:00 |
14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - | - | 14:00 – 18:00 |
Supporter timezone: Africa/Casablanca (GMT+01:00)
This topic contains 6 replies, has 2 voices.
Last updated by lucasD-4 4 years, 6 months ago.
Assisted by: Jamal.
I am currently trying out the toolset messaging module and wonder if it is possible to create groups so that multiple users can receive and reply to the message. I'd also like to see the number of unread messages or inform the user about a new message via a small notification box in the frontend top. Like for example "You have 1 new message". Does anyone have an idea if and how this can be implemented? If necessary, maybe with a third-party plugin.
Hello and thank you for contacting the Toolset support.
I believe you can implement this with Toolset and some custom code. You may want to use a 3rd party plugin and you may not. It will depend on your use case. I would suggest checking bbPress or BuddyPress if you want to create an online forum or community.
- hidden link
- hidden link
Or you can create it with, only, Toolset. As you may see in our tutorial on how to create a messaging system with Toolset. Each message is saved in the database as a post of the custom post type "Messages", and a notification is sent to the user from the "Message to" custom field
https://toolset.com/learn/how-to-create-a-messaging-system-with-toolset/ some features needed custom code to be implemented.
You may omit using a user on the "Message to" field, or it may hold a reference to the group where this message has been sent.
I'll try to comment on your first reply to give more ideas on how to do this.
I am currently trying out the toolset messaging module and wonder if it is possible to create groups so that multiple users can receive and reply to the message.
This will need to have a custom post type "Groups", the post can have a repeating field that will hold the members of the group.
Following our tutorial, each message will create a "message" post on the database and send a notification to the email of the "Message To:". In this case, we'll need to get the members of the group and send the message to all of them. We can use the hook cred_notification_recipients.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
I'd also like to see the number of unread messages or inform the user about a new message via a small notification box in the frontend top.
This might be a bit hard, but totally doable. You can analyze some plugins that provide this kind of feature, such as https://wordpress.org/plugins/mark-as-read/ The plugin seems very old but may give some inspiration.
I can think of two ways:
- 1st idea: duplicate each message to have one message per member of the group, then we can update a custom field "is read". We can also query messages by the custom field and "Message from", "Message to". This can be very complex.
- 2nd idea, simpler than the first: keep track of the user state(read/unread) by using a custom field that is composed of the user id. You can use generic fields to build such custom fields, and use the persist-key in the generic field JSON definition.
https://toolset.com/documentation/user-guides/front-end-forms/inserting-generic-fields-into-forms/
Toolset won't actually let you pull the value of these custom fields with views shortcodes. On the other hand, it allows you to use custom functions or custom shortcodes to get/set the value. Allows you hooks to filter a query and add a condition on the custom field "read_{my_id}".
- https://toolset.com/documentation/user-guides/views/views-shortcodes
- https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/using-shortcodes-in-conditions/
-https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/using-custom-functions-in-conditions/
To display the number of unread messages, a view can be used. Note that the view cannot be inserted on a regular WordPress menu. But you can hook into it programmatically, or render the view on the page and move it into the menu with Javascript code.
I hope this gives some insights on how to perform this. Let me know your feedback.
Thank you very much for your comprehensive answer!
I took a look at the plugin "Mark as read" and I think it might already be a big help to realize my idea. However, the plugin seems to only display information for standard WordPress posts and not for CPT. I haven't found out yet what I have to change in the code to display a list of unread posts for CPT "Messages". I suppose it's because the plugin filters the posts by category.
//get all unread posts in array function get_unread_posts($category = "all") { if ($category == "all") { $posts = get_posts( array( 'orderby' => 'modified' ) ); } else { $posts = get_posts( array( 'orderby' => 'modified', 'category' => $category ) ); } $return = array();
I am aware that we are talking about a third party plugin here and I can absolutely understand if you don't want or can't offer any help on this.
Thank you for your feedback and understanding. I believe this is a simple question and answering it will definitely help us help you 🙂
The get_posts function from WordPress can take all the arguments that WP_Query::parse_query() can take, post_type among them:
$posts = get_posts( array( 'orderby' => 'modified', 'post_type' => 'message', // <= custom post type slug ) );
- https://developer.wordpress.org/reference/functions/get_posts/
- https://developer.wordpress.org/reference/classes/wp_query/parse_query/
Thanks a lot! It's working fine now.
I have tried to create a shortcode that uses the sizeof() function to indicate the number of unread messages. Unfortunately it seems not to work, because it always outputs "1".
function count_unread_posts(){ $list_unread_posts = the_unread_posts(); return sizeof($list_unread_posts, COUNT_RECURSIVE); } add_shortcode('number_unread_messages' , 'count_unread_posts' );
I am not sure what the function "the_unread_posts" returns. If it is not an array, an object, for example, it will always return 1.
Check this screenshot hidden link
C:\Users\LENOVO>php -a Interactive shell php > $arr = array(1,2,3); php > echo sizeof( $arr); 3 php > echo count($arr); 3 php > echo sizeof($arr, COUNT_RECURSIVE); 3 php > echo COUNT_RECURSIVE; 1 php > $obj = (object) $arr; php > echo sizeof($obj, COUNT_RECURSIVE); Warning: sizeof(): Parameter must be an array or an object that implements Countable in php shell code on line 1 1
I'll suggest using print_r and var_dump to investigate the return value of the function. If you are not comfortable with PHP coding, you should probably hide a developer. Check a list of our partners here: https://toolset.com/contractors/
I did it! It was senseless to define new function and new variable. I added the return command to the existing function get_unread_posts() to output count(). Maybe this is not the most ideal way, but it works!
Thanks for your valuable tips and hints!