Message to Post_Author from a custom post field
-
Hello everyone,
I hope this message finds you well. I am experiencing a challenge while attempting to create a custom shortcode for the Better Messages plugin on my website. I’m hoping you could kindly assist me in resolving this issue.
The purpose of the custom shortcode is to add a message input field to the single post page. This field should allow users to send a direct private message to the author using the Better Messages plugin.The Shortcode which I have integrated into my theme by adding custom code to utilize the existing logic of the plugin for sending messages to post authors.
I successfully created another shortcode to initiate a chat with the author, even when a chat didn’t exist. This part of the code is functioning correctly. Then I wanted to extend my code so that a message from a field is sent automatically but I encountered some problems. So that’s the code which works fine.
/**Message now button shortcode*/ function author_message_now_button_shortcode(){ $author_id = get_post_field('post_author', get_the_ID()); $message_link = Better_Messages()->functions->private_message_link($author_id); if ($message_link) { echo '<a class="message-button" href="' . $message_link . '">Nachricht senden</a>'; } } add_shortcode('message_now', 'author_message_now_button_shortcode');Here’s a breakdown of the problem I encountered:
The issue arises when I attempt to add the shortcode for the message input field. Instead of displaying the expected input field, the shortcode output shows the text “Deleted user.” It seems that the threads from the plugin are not being properly incorporated into the new shortcode.
I have double-checked my code for errors but haven’t been able to pinpoint the exact problem. I believe there might be a compatibility issue or an error in how I’m trying to incorporate the existing plugin logic into the new shortcode.
Below is the current version of code I have used for the custom shortcode:
function send_message_to_author_shortcode() { // Check if the user is logged in if (!is_user_logged_in()) { return 'You must be logged in to send a message.'; } // Get the post author ID $author_id = get_post_field('post_author', get_the_ID()); // Check if the current user is the post author if ($author_id === get_current_user_id()) { return 'You cannot send a message to yourself.'; } // Handle the form submission if (isset($_POST['send_message'])) { // Prepare the message data $message_content = isset($_POST['message_content']) ? sanitize_textarea_field($_POST['message_content']) : ''; if (!empty($message_content)) { // Check if a conversation already exists between current user and post author $api = Better_Messages()->api; $threads = $api->get_threads(array('user_ids' => array(get_current_user_id(), $author_id))); if ($threads) { // If a conversation exists, use the first thread in the array $thread_id = $threads[0]->thread_id; } else { // If no conversation exists, create a new thread $thread_args = array( 'recipients' => array($author_id), 'subject' => 'Konversation mit ' . get_the_author(), ); $thread_id = $api->new_thread($thread_args); } // Send the message $message_args = array( 'sender_id' => get_current_user_id(), 'recipients' => array($author_id), 'thread_id' => $thread_id, 'subject' => 'Nachricht vom Leser: ' . get_the_title(), 'content' => $message_content, ); // Instanziere ein BM_Messages_Message-Objekt und sende die Nachricht $message_object = new BM_Messages_Message(); $message_object->populate(0); // Setze eine Dummy-ID für die neue Nachricht $message_object->recipients = $message_args['recipients']; $message_object->sender_id = $message_args['sender_id']; $message_object->thread_id = $message_args['thread_id']; $message_object->subject = $message_args['subject']; $message_object->message = $message_args['content']; // Sende die Nachricht $result = $message_object->send(); if ($result) { // Erfolgreich gesendet return 'Nachricht erfolgreich an den Autor gesendet.'; } else { // Fehler beim Senden der Nachricht return 'Fehler beim Senden der Nachricht.'; } } } ob_start(); ?> <form method="post"> <label for="message_content">Nachricht an den Autor:</label><br> <textarea id="message_content" name="message_content" rows="4" cols="50"></textarea><br> <input type="submit" name="send_message" value="Nachricht senden"> </form> <?php return ob_get_clean(); } add_shortcode('custom_message_field', 'send_message_to_author_shortcode');If anyone could offer guidance or assistance in resolving this issue, I would be extremely grateful. I am eager to get this functionality working as intended, and your expertise would be invaluable.
Thank you all for your time and help. If you need any additional information or code snippets, please let me know, and I’ll be happy to provide them.
Best regards
The topic ‘Message to Post_Author from a custom post field’ is closed to new replies.