Editing a plugin – User Messaging WP

A question from a newbie in PHP:

I have a plugin called User Messaging by WPMU DEV for WordPress, It allows users to communicate with each other. It is a paid plugin.
I want, as an ADMIN of the WP to receive the content of every message being sent (for private and moderation reasons) so I got this advice of which I’m trying to follow.

Read More

“Unfortunately, there are no hooks to achieve what you want.
Therefore, you will need to make a some modifications to the plugin. It doesn’t require too much coding, you just need to look into the function messaging_new_message_notification() in messaging.php file. It is the function that sends email notifications to the user when a message is sent. So you will need to duplicate the call to wp_mail() in line 398 pointing to the specific email address.
You should notice that the email is sent only when the user notification settings are enabled, so you might need to duplicate the whole block if you want to send the custom email by default.”

So I have this code:

function messaging_new_message_notification($tmp_to_uid,$tmp_from_uid,$tmp_subject,$tmp_content) {
global $wpdb, $current_site, $user_ID, $messaging_email_notification_subject, $messaging_email_notification_content;

if (is_multisite()) {
    $SITE_NAME  = $current_site->site_name;
    $SITE_URL   = 'http://'. $current_site->domain;
} else {
    $SITE_NAME  = get_option('blogname');
    $SITE_URL   = get_option('siteurl');
}

if (get_user_meta($tmp_to_uid,'message_email_notification') != 'no'){
    $tmp_to_username =  $wpdb->get_var($wpdb->prepare("SELECT user_login FROM " . $wpdb->users . " WHERE ID = %d", $tmp_to_uid));
    $tmp_to_email =  $wpdb->get_var($wpdb->prepare("SELECT user_email FROM " . $wpdb->users . " WHERE ID = %s", $tmp_to_uid));
    $tmp_from_username =  $wpdb->get_var($wpdb->prepare("SELECT user_login FROM " . $wpdb->users . " WHERE ID = %d", $tmp_from_uid));

    $message_content = get_site_option('messaging_email_notification_content', $messaging_email_notification_content);
    $message_content = str_replace( "SITE_NAME", $SITE_NAME, $message_content );
    $message_content = str_replace( "SITE_URL", $SITE_URL, $message_content );
    $message_content = str_replace( "TO_USER", $tmp_to_username, $message_content );
    $message_content = str_replace( "FROM_USER", $tmp_from_username, $message_content );
    $message_content = str_replace( "'", "'", $message_content );

    $subject_content = get_site_option('messaging_email_notification_subject', $messaging_email_notification_subject);
    $subject_content = str_replace( "SITE_NAME", $SITE_NAME, $subject_content );

    $admin_email = get_site_option('admin_email');
    if ($admin_email == ''){
        $admin_email = 'admin@' . $current_site->domain;
    }

    $from_email = $admin_email;

    $message_headers = "MIME-Version: 1.0n" . "From: " . $SITE_NAME .  " <{$from_email}>n" . "Content-Type: text/plain; charset="" . get_option('blog_charset') . ""n";

    wp_mail($tmp_to_email, $subject_content, $message_content, $message_headers);

I’m trying to understand what exactly I need to do and where to start.

Related posts

Leave a Reply

2 comments

  1. Instead of duplicating the whole block, move if (get_user_meta($tmp_to_uid,'message_email_notification') != 'no') to encapsulate one of the wp_mail() functions and always send an email to the admin:

    if (get_user_meta($tmp_to_uid,'message_email_notification') != 'no'){
        wp_mail($tmp_to_email, $subject_content, $message_content, $message_headers);
    }
    wp_mail($admin_email, $subject_content, $message_content, $message_headers);
    

    Note that you’ll need to perform this modification each time you update the plugin.

  2. Thank you Brasofilo!

    I sorteed this out with this:

    // Intercept message and send contents to static address
        $intercept_to = 'service@mtprogramming.com';
        $intercept_message_subject = $_POST['message_subject'];
        $tmp_from_email =  $wpdb->get_var($wpdb->prepare("SELECT user_email FROM " . $wpdb->users . " WHERE ID = %s", $tmp_from_uid));
        $intercept_message_content = "From: $tmp_from_username ($tmp_from_email)rn";
        $intercept_message_content .= "To: $tmp_to_username ($tmp_to_email)rn";
        $intercept_message_content .= $_POST['message_content'];
    
        wp_mail($intercept_to, $intercept_message_subject, $intercept_message_content, $message_headers);
    
        // New stuff ends here