Skip to main content

get_private_conversation_id

This function is to get existing or create new private conversation with user

REQUIREMENTS

To be able to implement this guide, you need to learn how to insert PHP snippets to your website.

You can find guide here: WP Beginner

Compatibility
This function compatible with Better Messages 2.0.64 or higher
<?php
// Recipient user
$to = 3;
// Starter of conversation in case it does not exist yet
$from = get_current_user_id();
// Create conversation if it does not exist
//If set to false function will not create conversation if it does not exist
$create = true;
//Used only when conversation not found and will be created
$subject = 'New conversation subject';

// The function returns array with results:
$result = Better_Messages()->functions->get_private_conversation_id( $to, $from, $create, $subject );

switch( $result['result'] ){
/*
If conversation exists
Array
(
[result] => thread_found
[thread_id] => 6608
)

If conversation not exists, but just created
Array
(
[result] => thread_сreated
[thread_id] => 6608
)
*/
case 'thread_created':
case 'thread_found':
$thread_id = $result['thread_id'];

// Do something with thread ID

// For example you can send message

/* $new_message_id = Better_Messages()->functions->new_message([
'sender_id' => $from,
'content' => 'message content',
'thread_id' => $thread_id,
'return' => 'message_id',
]); */

break;

/*
It's possible that current user cant create new conversation with another user, due to some restrictions settings in the plugin.
Then it will return error.

Array
(
[result] => not_allowed
[errors] => ["Error message 1", "Error message 2"]
)
*/
case 'not_allowed':
// Process errors
$errors = $result['errors'];

foreach( $errors as $error ){
// echo $error . '<br>';
}

break;
}