Develop

BuddyPress: Send Admin Notification when User Updates Extended Profile Fields

Background

I wrote a similar post explaining how to send an email notification from WordPress when standard and custom profile fields are updated. A commenter on that post was asking if this was possible with the extended profile fields that you setup in BuddyPress. Well, of course anything is possible…it’s just a matter of figuring it out ;)

Solution

In this example I setup a custom profile field in BuddyPress named “Color”. The code below will send an email if a member changes that field.

  1. Place this code in your functions.php file or into your custom functionality plugin if you’ve created one.
  2. Add the email address you want there alerts to go to on line 3.
  3. Add your field’s name in both places on line 4 that say “Color” (your field name should appear exactly the way you wrote it when you created the field).
  4. If you want to change the subject of the email, you can do that on line 5.
function rc_buddypress_profile_update( $user_id ) { 

     $admin_email = "YOUR-EMAIL@DOMAIN.COM";
     $message = sprintf( __( 'Member: %1$s', 'buddypress' ), bp_core_get_user_displayname( $user_id ) ) . "\r\n\r\n"; 
     $message .= sprintf( __( 'Color: %s' ), bp_get_profile_field_data('field=Color') ). "\r\n\r\n";
     wp_mail( $admin_email, sprintf( __( '[YOUR SITE] Member Profile Update' ), get_option('blogname') ), $message );
 }

 add_action( 'xprofile_updated_profile', 'rc_buddypress_profile_update', 10, 5 );

{ 9 Comments }

  1. Antonio

    It works for me. Thank you very much!!!

  2. Daniel

    Too cool! Thanks for this! :)

    In your other example you provided instances for multiple fields that showed the old and the new and would send a separate email only if that field was changed.

    Would it be difficult to do that in this case?

    Note: I’m not a seasoned “PHPer” ;)

  3. Greetings,
    Is it possible to include all the profile details in the new registration email sent to admin. If yes, how? Sorry I am a newbie.
    Thanks in advance

  4. Hey Chris,

    I have a few questions about this function. Specifically, for line 5 do I need to include every single field? Also, for the bit of code that preceeds bp_get_profile_field_data, .= sprintf( __( ‘Color: %s’ ). Do I need to include anything in here, next to ‘Color:%s’ or leave it as is?

    For line 6, do I need to type the captitalized [YOUR SITE] exactly like this [INTIMATENCOUNTERS]?

    And lastly, for ‘blogname’ would I type ‘intimatencounters’ ?

    function rc_buddypress_profile_update( $user_id ) {

    $admin_email = “admin@intimatencounters.com”;
    $message = sprintf( __( ‘Member: %1$s’, ‘buddypress’ ), bp_core_get_user_displayname( $user_id ) ) . “\r\n\r\n”;
    $message .= sprintf( __( ‘Color: %s’ ), bp_get_profile_field_data(‘field=Color’) ). “\r\n\r\n”;
    wp_mail( $admin_email, sprintf( __( ‘[YOUR SITE] Member Profile Update’ ), get_option(‘IntimatEncnounters’) ), $message );
    }

    add_action( ‘xprofile_updated_profile’, ‘rc_buddypress_profile_update’, 10, 5 );

    • Chris Perryman

      I emailed you about the solution we discussed on the phone ;)

  5. betro

    Thank you for this! I got it to work when the field is just text or a radio button. When the user updates a field that has checkboxes, it emails this:

    “Days available: Array”

    instead of this:

    “Days available: Monday, Thursday, Friday”

    Is there a way to get the array values emailed as well?

    • Chris Perryman

      It’s returning “Array” because you have multiple values – I don’t have time to troubleshoot the correct code for you right this second, but off the top of my head you would need to do a “foreach” in order to spit out all of the values.

{ Respond }

Leave a response