Skip to:
Content
Pages
Categories
Search
Top
Bottom

Filtering E-mail Message Content


  • adamt19
    Participant

    @adamt19

    I’m trying to filter the content (and eventually the subject line) for “new message” e-mail notifications. Reading some other threads here got me this far:

    function mm_new_message_message($email_content, $sender_name, $subject, $content, $message_link, $settings_link, $sender_id, $ud) {
    	
      	
      $sender = new WP_User($sender_id);
      $sender_login = stripslashes($sender->user_login);
    
      $user_login = $ud->user_login;
    	
    	// Set up and send the message
      ob_start();
    
      // template stack, using vars above
      include( 'email_notif_header.php' );
      include( 'email_notif_bp_message_notification.php' );
      include( 'email_notif_footer.php' );
    
      $email_content = ob_get_contents();
      ob_end_clean(); 
    
      return $email_content;
     
    	
    }
    add_filter( 'messages_notification_new_message_message', 'mm_new_message_message', 1, 8);

    Issue(s):
    We’re not using sender_names anywhere. We’d like to use the sender’s username, and also include a link to that sender’s profile. But I can’t get the sender’s username right now with the code above.

    I’m also having trouble getting the username of the recipient. The link to their messages works fine since it’s passed in directly, but I can’t get the username itself.

    So the sender/sender_login/user_login stuff isn’t working, but the $content and $subject is working just fine, of course, since I’m just receiving it from the original function. Any suggestions on how to get the additional elements I need?

    Finally, is there a way to directly link to the message thread referenced in the notification? There’s another thread about this in the support forums but it ended with a request that the OP request this in trac.buddypress.

Viewing 5 replies - 1 through 5 (of 5 total)

  • adamt19
    Participant

    @adamt19

    Can’t determine if I’m supposed to use:

    mm_new_message_message($raw_args = array()) {} …and try to get the usernames from the sender/recipient ID (not working no matter what I try)

    or

    mm_new_message_message($email_content, $sender_name, $subject, $content, $message_link $settings_link){} ..which doesn’t seem to offer any opportunity to get usernames, as sender_name can’t be reverse engineered (that I know of) and there’s no recipient info passed this way

    or

    mm_new_message_message($email_content, $sender_name, $subject, $content, $message_link $settings_link, $sender_id, $recipient){} .. which isn’t working either no matter what I do in trying to get sender IDs/usernames or recipient ids/usernames, probably because I only have available to me the original 6 parameters and adding the two on the end is useless?

    Banging my head against the wall here on this, any help MUCH appreciated!


    adamt19
    Participant

    @adamt19

    half solved.. of course I can get the sender’s username with:
    $sender_username = bp_core_get_username(bp_loggedin_user_id())

    but I can’t use displayed_user_id for the recipient’s ID, because when you send messages you send them from your own profile.

    So, still looking for a way to get the recipient’s ID (and therefor, their username using bp_core_get_username($theid)


    shanebp
    Moderator

    @shanebp

    >adding the two on the end is useless

    You can’t add new args to an apply_filters call.

    You might try using the first filter in wp_mail()
    https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/pluggable.php#L225

    And then use the ‘to’ value to query the database directly to retrieve the recip’s username.
    Look at https://codex.wordpress.org/Class_Reference/wpdb, in particular $wpdb->get_var

    Then you can rebuild ‘message’.

    Of course the add_filter function needs a conditional check so that you don’t mess with other wp-mail calls.
    I do something like:
    -use one of the filters in function messages_notification_new_message to add a keyword
    -check for that keyword in your filter function for wp_mail()

    btw- wp_mail is pluggable, so you can overload it with your own function, but that shouldn’t be necessary.


    shanebp
    Moderator

    @shanebp

    Yes, the above is a rather awkward solution.

    Just occurred to me that you could extract the info you need from the $settings_link arg passed by the $email_content filter hook to further query the database and/or rebuild $email_content – and so avoid filtering wp_mail

    I think it would be a reasonable request that the
    $email_content filter hook in messages_notification_new_message include $ud.

    It’s a simple change, so please submit an enhancement ticket:
    https://buddypress.trac.wordpress.org/

    [ We may find out that there is an existing and simple solution ! ]


    adamt19
    Participant

    @adamt19

    I think so too! 🙂 Will submit a ticket

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Filtering E-mail Message Content’ is closed to new replies.
Skip to toolbar