BuddyDev

Search

[Resolved] Send email to all featured members when a site wide notice is published

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #38531

    Hello dear team,

    I would like to send an email to all featured users for our website, when a sidewide notice have been published so that they know about it.

    I managed to write this little code

     function important_notification() {
        
        $notification = '' . bp_get_message_notice_text( $notice->message ) . '';
    
        $args = array(
        'meta_key'     => '_is_featured'
        );
        
    
        $members = get_users( $args );
        foreach( $members as $member ) {
        $user_id = $member->ID;
        $member_account = bp_core_get_user_domain( $user_id );
        
        $first_name = $member->first_name;
    
        $email_subject = 'Important information';
    
        $message = sprintf( 'Hello %1$s,' , $first_name ) . "<br><br>"; 
       
        $message .= sprintf( 'We have an important information for you:' ) ."<br><br>"; 
        $message .= sprintf( '<i>%1$s</i>' , $notification ) ."<br><br>";
        $message .= sprintf( 'Please login <a href="%1$s" target="_blank">to your account</a> to remove the information if you want.' , $member_account ) ."<br><br>";
        
    
        
        wp_mail( $member->user_email, $email_subject, $message );
        }
    }
    add_action(  'xxxxxxxx',  'important_notification'); 

    My questions are
    -will the $notification = '' . bp_get_message_notice_text( $notice->message ) . ''; get the message text of the sidewide notice?
    -in which action could i hook this function in order to send an email when admin will publish a sidewide notification?

    thank you very much for your time.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #38533

    Hello,

    Thank you for posting. Please try the following action:

    
    messages_send_notice
    

    It will be called when the notice successfully sent.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #38534

    Thank you @Ravi for your help 🙂

    I wll test that.

    Do you know maybe how can i get the text of the message?
    I have tried some testing with different variations like

     global $messages_template;
    echo '' . bp_message_notice_text($messages_template->thread->message) . '';

    or

    global $notice;
     echo '' . bp_get_message_notice_text( $notice->message ) . '';

    but i get Warning: Creating default object from empty value.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #38537

    Hello,

    Please try the following code:

    
    
    /**
     * Notify featured members
     *
     * @param string $subject Notice Subject.
     * @param string $message Notice Message.
     */
    function buddydev_notify_featured_members( $subject, $message ) {
    	// Send email here.
    }
    
    add_action( 'messages_send_notice', 'buddydev_notify_featured_members', 10, 2 );
    
    

    Please let me know if it works or not.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #38559

    Hello @Ravi,

    The action works correct .

    My only issue is how can i get the text of the active notice in the loop?

    `function important_notification($subject, $message) {
    $notice = BP_Messages_Notice::get_active();
    $notification = ” . bp_message_notice_text($notice) . ”;

    $args = array(
    ‘meta_key’ => ‘_is_featured’
    );

    $members = get_users( $args );
    foreach( $members as $member ) {
    $user_id = $member->ID;
    $member_account = bp_core_get_user_domain( $user_id );

    $first_name = $member->first_name;

    $email_subject = ‘Important information’;

    $note_message = sprintf( ‘Hello %1$s,’ , $first_name ) . “<br><br>”;

    $note_message .= sprintf( ‘We have an important information for you:’ ) .”<br><br>”;
    $note_message .= sprintf( ‘<i>%1$s</i>’ , $notification ) .”<br><br>”;
    $note_message .= sprintf( ‘Please login <a href=”%1$s” target=”_blank”>to your account</a> to remove the information if you want.’ , $member_account ) .”<br><br>”;

    wp_mail( $member->user_email, $email_subject, $note_message );
    }
    }
    add_action( ‘messages_send_notice’, ‘important_notification’, 10, 2 );`

    If i use the `$notice = BP_Messages_Notice::get_active();
    echo ” . bp_message_notice_text($notice) . ”;` in a page i get the text normal. But inside a plugin file it doesn’t work. What am i missing ?

    Thank you for your time and your great support.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #38560

    Hello Lefteris,

    Thank you for your acknowledgment of the action. Instead of “bp_message_notice_text($notice)” you can use “$subject” for notice subject and “$message” for notice message passed as a parameter in the callback function. Please give it a shot this way. Please let me know if it works or not. If still faces the issue please share code on https://pastebin.com/ so that I can help.

    Regards
    Ravi

    • This reply was modified 3 years, 6 months ago by Ravi.
  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #38619

    Hello @Ravi,

    You were correct. I spotted my mistake. It’s working now! Thank you for your help!!

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #38620

    Hello Lefteris,

    Thank you for the acknowledgment. I am glad that I could help.

    Regards
    Ravi

The topic ‘ [Resolved] Send email to all featured members when a site wide notice is published’ is closed to new replies.

This topic is: resolved