Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to restrict PM notifications email to excerpt only


  • Pieter
    Participant

    @pieter

    WP 4.1.1
    BP 2.2.1

    I would like email notifications having only an excerpt of personal messages, instead of the whole text.
    Excerpts only will force recipients to click on the link and visit the site, instead of replying via email to the wrong address (of WP site administrator).

    There’s no way to edit that in pref panel, right?
    Who knows the magic trick to do this?
    Thanks.

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

  • Henry Wright
    Moderator

    @henrywright

    Hi @pieter

    Try this

    function filter_private_message_email_body( $email_content, $sender_name, $subject, $content, $message_link, $settings_link, $ud ) {
    
        $content = substr( $content , 0, 140 );
    
        $email_content = sprintf( __(
    '%1$s sent you a new message:
    Subject: %2$s
    "%3$s"
    To view and read your messages please log in and visit: %4$s
    ---------------------
    ', 'buddypress' ), $sender_name, $subject, $content, $message_link );
    
        return $email_content;
    }
    add_filter( 'messages_notification_new_message_message', 'filter_private_message_email_body', 10, 7 );

    This will make the message 140 characters. Feel free to change the 140 to another number.


    shanebp
    Moderator

    @shanebp

    @henrywright

    This is a bit nicer:
    $content = wp_trim_words( $content, 15 );

    https://codex.wordpress.org/Function_Reference/wp_trim_words


    Pieter
    Participant

    @pieter

    Thanks a lot both of you.
    Works like a charm. 🙂


    Henry Wright
    Moderator

    @henrywright

    Thanks for pointing out wp_trim_words()!


    maelga
    Participant

    @maelga

    How can I add “Dear username” at the beginning of the email message?

    I have tried adding $user_id to the function with ` $user = get_userdata( $user_id );
    ‘Dear ‘ . $user->user_login . ‘,`
    but it does not return any value.

    Any idea?


    Henry Wright
    Moderator

    @henrywright

    $ud is a WP_User object of the person who is receiving the message. So:

    function filter_private_message_email_body( $email_content, $sender_name, $subject, $content, $message_link, $settings_link, $ud ) {
    
        $content = wp_trim_words( $content, 15 );
    
        $email_content = sprintf( __(
    'Dear %1$s,
    %2$s sent you a new message:
    Subject: %3$s
    "%4$s"
    To view and read your messages please log in and visit: %5$s
    ---------------------
    ', 'buddypress' ), $sender_name, $subject, $content, $message_link, $ud->user_login );
    
        return $email_content;
    }
    add_filter( 'messages_notification_new_message_message', 'filter_private_message_email_body', 10, 7 );

    maelga
    Participant

    @maelga

    Thanks @henrywright.

    The order of the variables should read $ud->user_login, $sender_name, $subject, $content, $message_link


    Henry Wright
    Moderator

    @henrywright

    Of course, sorry. Glad you figured it out 🙂


    Pieter
    Participant

    @pieter

    This stopped working, even in BP 2.5.3
    Should I upgrade to 2.6 to get it back?


    Pieter
    Participant

    @pieter

    This is not working anymore, even with the latest ugrades to BP 2.6.1.1 and WP 4.5.3.
    Also the emails arrive in non-html version.

    Who can clear this up?

    Thanks in advance,
    Pieter


    dsnic
    Participant

    @dsnic

    I would also like to know this. I used the code from this thread and it worked fine, but now it has stopped working. Help?


    empressdianna
    Participant

    @empressdianna

    I don’t have an exact fix but I went into “Emails” from the WordPress Sidebar and edited the email that is sent on a new message sent trigger. I just took out the {{subject}} and {{quot}} tags and then just put a message that they have to login to view the message. hth


    danbp
    Moderator

    @danbp

    Hi @empressdianna,

    You did it right ! Thanks for sharing. I close this topic now, as initial issue is now resolved.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘How to restrict PM notifications email to excerpt only’ is closed to new replies.
Skip to toolbar