For a project where messages had to be sent automatically to the users without additional email being sent, I’ve chosen to use private messages for this purpose.

In this project when a user uploads a new file, a private message should be sent to another user. This way there is not a flood of annoying emails. Ultimate Member will send an email if the receiving user does not log in to check his private messages. This email will normally be sent once a day.

In the code where the update is received, I add this function:

rsp_send_private_message($to, $from, $message);

 /** 
  * Send a private message from a user to a user 
  * 
  * @since   
  * @param  int $to   User id the message is directed to
  * @param  int $from User id the message should come from
  * @param  string $message The text message
  * @return bool             void 
*/
function rsp_send_private_message($to, $from, $message){
  //We do not want to message ourselves
  if ($from==$to) return;
  // Create conversation and add message
  $_POST['content']  = "Automatically generated message: " . $message;
  $conversation_id = UM()->Messaging_API()->api()->create_conversation( $to, $from );;
  $_POST['content'] = "";
  do_action('um_after_new_message', $to, $from, $conversation_id );
}

11 Responses

    1. Hi Ivan,

      this likely has to do with the way ultimate member handles roles, in UM 2.0. most roles have an added um_ before the role and therefore are different from roles in UM 1. I will look into this issue and get back to you.

        1. Hi Ivan,

          UM2 uses a different class structure, therefore the $um_messaging->api-> doesn’t work anymore. This can be replaced by UM()->Message_API->api()->. The globals can be removed as well, I have updated the article to use UM2 code.

          1. Unfortunately, the new code also does not work. Problem in the same line: $conversation_id =UM()->Message_API->api()->create_conversation( $to, $from );

        2. Hi Ivan,

          I have just tested this code again in UM2.0, and it works fine. Do you have the private messages add-on from Ultimate Member installed? If so, what error are you seeing?

          1. Yes, the latest version is installed.

            WordPress, debug.log:
            PHP Notice: Undefined property: UM::$Message_API
            PHP Fatal error: Uncaught Error: Call to a member function api() on null

        3. The error you posted indicates the private messaging plugin from UM is not activated. If you have the plugin
          “Ultimate Member – Private Messages” installed, can you check
          – if it is also the 2.0 version, just as Ultimate Member
          – if it is activated

          1. Ultimate Member v. 2.0.23,
            Ultimate Member – Private Messages v. 2.0.7, activated, of course.

            If comment out the line “$conversation_id =…”, the message comes, but it’s empty.

          2. You are right, I have found the problem: there was a typo in the code: Message_API has to be Messaging_API.

            I have update this line. It should now work!

Leave a Reply

Your email address will not be published. Required fields are marked *