Title: Initialize BM with Javascript
Last modified: April 14, 2023

---

# Initialize BM with Javascript

 *  [razvanfrandes](https://wordpress.org/support/users/razvanfrandes/)
 * (@razvanfrandes)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/initialize-bm-with-javascript/)
 * Hello,
 * How to load a thread only by id ? 
   Or how to load the whole bm using javascript(
   Lets supose I have the html strucure) ?
 * I tried to read the code, the only thing I found is jQuery(document).on(“bp-better-
   messages-init-scrollers”). Which somehow reinit the bm.
   What I want to achive
   is trough rest api endpoint the user will get a thread_id, that will load the
   BM.Tried to echo the shordcode in the rest_api, but ofc it will echo only html
   strucute without content, the content is updated by javascript.
 * This works, but ofc only in php do_shortcode(‘[better_messages_single_conversation
   thread_id=”8″]’)
   The code is a whole book, can’t seem to find by watchin in source
   tabs. Thank you.

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Plugin Author [wordplus](https://wordpress.org/support/users/wordplus/)
 * (@wordplus)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/initialize-bm-with-javascript/#post-16655664)
 * Hi there!
   Which html code do you have? So I have understanding
 *  Thread Starter [razvanfrandes](https://wordpress.org/support/users/razvanfrandes/)
 * (@razvanfrandes)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/initialize-bm-with-javascript/#post-16655676)
 * Hey 🙂
   Thank you for the quick response.In this gist you will find the html.[https://gist.github.com/razvanFrandes/fcfbf302809101b71ec2cdc828349515](https://gist.github.com/razvanFrandes/fcfbf302809101b71ec2cdc828349515)
 * The html is outputed, by the rest api.
 *  Plugin Author [wordplus](https://wordpress.org/support/users/wordplus/)
 * (@wordplus)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/initialize-bm-with-javascript/#post-16655732)
 * Did you tried this?
   jQuery(document).trigger(“bp-better-messages-init-scrollers”);
 *  Thread Starter [razvanfrandes](https://wordpress.org/support/users/razvanfrandes/)
 * (@razvanfrandes)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/initialize-bm-with-javascript/#post-16655761)
 * Dam…
 * It was having in my response converted to response.text 😐
 * This code works
   Thank you for your time, awesome plugin! Waiting for vanila/react
   version 🙂(I know it;s build with react/redux to bad we can’t hook into many 
   things)
 *     ```wp-block-code
       function updateShortcode(thread_id) {
   
         fetch("/api/update_bm", {
   
           method: "POST",
   
           headers: {
   
             "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
   
           },
   
           body: new URLSearchParams({ thread_id: thread_id }),
   
         })
   
           .then((response) => response.json())
   
           .then((html) => {
   
             const container = document.querySelector("#shortcode-container");
   
             container.innerHTML = html;
   
             console.log(html);
   
             jQuery(document).trigger("bp-better-messages-init-scrollers");
   
           })
   
           .catch((error) => {
   
             console.error("Error updating shortcode:", error);
   
           });
   
       }
   
       document.querySelector("#messages").addEventListener("click", function () {
   
         var id = 10;
   
         updateShortcode(id);
   
       });
       ```
   
    -  This reply was modified 3 years, 1 month ago by [razvanfrandes](https://wordpress.org/support/users/razvanfrandes/).
 *  Plugin Author [wordplus](https://wordpress.org/support/users/wordplus/)
 * (@wordplus)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/initialize-bm-with-javascript/#post-16656059)
 * Hi there!
   Redux is not used here, if you need hooks, feel free to requests them.
 *  Thread Starter [razvanfrandes](https://wordpress.org/support/users/razvanfrandes/)
 * (@razvanfrandes)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/initialize-bm-with-javascript/#post-16693299)
 * Thank you!
   For example I have 2 situations.
    1. How can I hook into when a new message is read?
    2. How to edit the email template, I need custom templating with php.
 * Thank you again. 
   Much apreciated!
 *  Plugin Author [wordplus](https://wordpress.org/support/users/wordplus/)
 * (@wordplus)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/initialize-bm-with-javascript/#post-16693418)
 * Hi there!
   1. Not sure I understand2. [https://www.better-messages.com/docs/development/guides/overwrite-email](https://www.better-messages.com/docs/development/guides/overwrite-email)
 *  Thread Starter [razvanfrandes](https://wordpress.org/support/users/razvanfrandes/)
 * (@razvanfrandes)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/initialize-bm-with-javascript/#post-16694389)
 * Hello 😀
    1. For example if I send a message to you. That message status is unread until 
       you view it. When you view the message I need to hook into it, for example when
       you view my message a email is sent to me that you have read my message.
 * Thank you 🙂
 *  Plugin Author [wordplus](https://wordpress.org/support/users/wordplus/)
 * (@wordplus)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/initialize-bm-with-javascript/#post-16696319)
 * WebSocket or AJAX?
 *  Thread Starter [razvanfrandes](https://wordpress.org/support/users/razvanfrandes/)
 * (@razvanfrandes)
 * [3 years ago](https://wordpress.org/support/topic/initialize-bm-with-javascript/#post-16713490)
 * Sorry for my late response.
   I am using ajax.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Initialize BM with Javascript’ is closed to new replies.

 * ![](https://ps.w.org/bp-better-messages/assets/icon-256x256.png?rev=2835105)
 * [Better Messages - Chat Rooms, Group Chat, Private Messages & AI Chat Bots](https://wordpress.org/plugins/bp-better-messages/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/bp-better-messages/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/bp-better-messages/)
 * [Active Topics](https://wordpress.org/support/plugin/bp-better-messages/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/bp-better-messages/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/bp-better-messages/reviews/)

 * 10 replies
 * 2 participants
 * Last reply from: [razvanfrandes](https://wordpress.org/support/users/razvanfrandes/)
 * Last activity: [3 years ago](https://wordpress.org/support/topic/initialize-bm-with-javascript/#post-16713490)
 * Status: not resolved