Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Notify by Email when xProfile Updated

  • Avatar of whedlund
    whedlund
    Participant

    @whedlund

    I am trying to write a function that will send me an email whenever a user makes a change to one of their profile fields. Thanks to Chris Perryman at Revelation Concept I was able to figure out how to do this when the user makes changes to a standard wordpress profile field like nickname or user_email.

    But she and I both got stuck trying to apply her awesome solution to buddypress xprofile fields. Here’s the thread from yesterday’s conversation between she and I.

    I would be super grateful if anyone has any ideas on what we’re missing. Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Avatar of whedlund
    whedlund
    Participant

    @whedlund

    To simplify my question, I’ve pasted the code in that Chris wrote. I just don’t know how to call the buddypress xprofile fields?

    /* ===========================================
    Send Emails when User Profile Changes
    =============================================*/

    // IF EMAIL CHANGES
    function sr_user_profile_update_email( $user_id, $old_user_data ) {

    $user = get_userdata( $user_id );
    if($old_user_data->user_email != $user->user_email) {
    $admin_email = "email@yourdomain.com";
    $message = sprintf( __( 'This user has updated their profile on the SchoolRise USA Staff Member site.' ) ) . "\r\n\r\n";
    $message .= sprintf( __( 'Display Name: %s' ), $user->display_name ). "\r\n\r\n";
    $message .= sprintf( __( 'Old Email: %s' ), $old_user_data->user_email ). "\r\n\r\n";
    $message .= sprintf( __( 'New Email: %s' ), $user->user_email ). "\r\n\r\n";
    wp_mail( $admin_email, sprintf( __( '[Staff Member Site] User Profile Update' ), get_option('blogname') ), $message );
    }
    }

    // Save old user data and meta for later comparison for non-standard fields (phone, address etc.)
    function sr_old_user_data_transient(){

    $user_id = get_current_user_id();
    $user_data = get_userdata( $user_id );
    $user_meta = get_user_meta( $user_id );

    foreach( $user_meta as $key=>$val ){
    $user_data->data->$key = current($val);
    }

    // 1 hour should be sufficient
    set_transient( 'sr_old_user_data_' . $user_id, $user_data->data, 60 * 60 );
    }
    add_action('show_user_profile', 'sr_old_user_data_transient');

    // Cleanup when done
    function sr_old_user_data_cleanup( $user_id, $old_user_data ){
    delete_transient( 'sr_old_user_data_' . $user_id );
    }
    add_action( 'profile_update', 'sr_old_user_data_cleanup', 1000, 2 );

    Avatar of shanebp
    shanebp
    Moderator

    @shanebp

    The hook you want is xprofile_updated_profile

    Full hook:
    do_action( 'xprofile_updated_profile', bp_displayed_user_id(), $posted_field_ids, $errors, $old_values, $new_values );

    See buddypress\bp-xprofile\bp-xprofile-screens.php

    [ Please use a service like gist or pastebin to share large code segments. ]

    Avatar of whedlund
    whedlund
    Participant

    @whedlund

    Thanks for the tip about gist or pastebin.

    Unfortunately, I’m not a php guy (I used to code in VBA) so I don’t know enough about what you just gave me to make it work. I think I still have to call the fields, or is it going to be a total rewrite of the code example I gave above to make this work?

    Avatar of shanebp
    shanebp
    Moderator

    @shanebp

    Total rewrite.

    The hook runs after a BP profile is updated.
    Write a function to see what was changed and then maybe send an email.

    This should get you started…

    function whelund_profile_updated(bp_displayed_user_id(), $posted_field_ids, $errors, $old_values, $new_values) {
      // dump the vars for orientation on what to write in the function
      var_dump( $posted_field_ids );
      // decide whether to send an email, then use wp_email() 
    }
    add_action( 'xprofile_updated_profile', 'whelund_profile_updated', 1, 5 );
    Avatar of RevConcept
    RevConcept
    Participant

    @revconcept

    Just wanted to follow up…we did come up with a solution for this in case any one else is looking for it.

    Full details are now on my blog at Revelation Concept.

    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 );
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘[Resolved] Notify by Email when xProfile Updated’ is closed to new replies.