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.
“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.
Instead of duplicating the whole block, move
if (get_user_meta($tmp_to_uid,'message_email_notification') != 'no')
to encapsulate one of thewp_mail()
functions and always send an email to the admin:Note that you’ll need to perform this modification each time you update the plugin.
Thank you Brasofilo!
I sorteed this out with this: