编辑插件 - 用户消息WP

时间:2014-04-24 15:59:53

标签: php wordpress email wordpress-plugin

来自PHP新手的问题:

我有一个名为User Messaging的插件,由WPMU DEV for Wordpress,它允许用户相互通信。这是一个付费插件。 我希望,作为WP的ADMIN,接收发送的每条消息的内容(出于私人和审核的原因),所以我得到了我想要遵循的建议。

  

“不幸的是,没有任何钩子可以达到你想要的效果。   因此,您需要对插件进行一些修改。它不需要太多编码,您只需要查看messaging.php文件中的messaging_new_message_notification()函数。它是在发送消息时向用户发送电子邮件通知的功能。因此,您需要在第398行复制指向特定电子邮件地址的wp_mail()调用。   您应该注意到,只有在启用了用户通知设置时才会发送电子邮件,因此如果您希望默认发送自定义电子邮件,则可能需要复制整个块。“

所以我有这段代码:

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.0\n" . "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);

我正在努力了解我需要做什么以及从哪里开始。

2 个答案:

答案 0 :(得分:0)

不要复制整个块,而是移动if (get_user_meta($tmp_to_uid,'message_email_notification') != 'no')以封装其中一个wp_mail()函数,并始终向管理员发送电子邮件:

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);

请注意,每次更新插件时都需要执行此修改。

答案 1 :(得分:0)

谢谢Brasofilo!

我用这个来解决这个问题:

// 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)\r\n";
    $intercept_message_content .= "To: $tmp_to_username ($tmp_to_email)\r\n";
    $intercept_message_content .= $_POST['message_content'];

    wp_mail($intercept_to, $intercept_message_subject, $intercept_message_content, $message_headers);

    // New stuff ends here