Skip to:
Content
Pages
Categories
Search
Top
Bottom

Backend notification when user profile complete

  • Avatar of guitfu
    Justin
    Participant

    @justineterniawebcom

    I am helping a nonprofit I support by building a site totally built on Buddypress: http://parishcollective.org/beta

    We are allowing users to create a profile with minimal information (via Social Login) but we want to keep the user role at ‘subscriber’ until they fill out the required profile fields. At that point, we need to be notified that they updated their info so an admin can check their info and up their role to ‘contributor.’ (It gives them access to creating blog posts and other things)

    Initially I started with something basic from this post: http://buddypress.org/support/topic/notification-on-profile-update-2/

    However, wp_mail() is conflicting with something in our site and causing a fatal error.

    So I’ve been attempting to add a user meta key (when a user updates their profile) that can be queried in the backend in a custom admin menu so the users with completed profiles can be displayed.

    Here was the adapted code based on the post above:

    
    
    function my_update_profile_mark() {
    $user_ID = get_current_user_id();
    $power_level = 9000;
    add_user_meta( $user_ID, '_level_of_power', $power_level);
    }
    add_action( ‘xprofile_updated_profile’, ‘my_update_profile_mark’ );
    
    

    If I understand the ‘xprofile_updated_profile’ hook correctly, it should just fire anytime any field is updated on a profile, right?

    From there I planned to experiment with something like this based on the Force Profile Update plugin to make the function fire when the profile is totally updated:

    function my_update_profile_mark() {
    
                            global $wpdb;
    
    			$bp_prefix = bp_core_get_table_prefix();
    			$xprofile_fields = $wpdb->get_results("SELECT count(*) AS empty_fields_count FROM {$bp_prefix}bp_xprofile_fields WHERE parent_id = 0 AND is_required = 1 AND id NOT IN (SELECT field_id FROM {$bp_prefix}bp_xprofile_data WHERE user_id = {$user_id} AND valueIS NOT NULL AND value` != '')");
    
    			foreach ($xprofile_fields as $field) 
    			{
    				if ($field->empty_fields_count > 0)	
    				{
                                      $user_ID = get_current_user_id();
                                      $power_level = 9000;
                                      add_user_meta( $user_ID, '_level_of_power', $power_level);
                                    }
    add_action( ‘xprofile_updated_profile’, ‘my_update_profile_mark’ );
    

    I’m a pretty new to programming and I don’t totally understand all the code here, but I’m trying hard to make something work here. Any ideas/guidance?

    I also realize this solution, even if it worked, would update the meta key ‘_level_of_power’ to the specified value “9000″ every time the user updated their profile after their profiles were filled out. But that’s an annoyance we can live with for now. Not knowing the user is ready to be upgraded wouldn’t work at all for the site. Any help would be appreciated. :(

Viewing 12 replies - 1 through 12 (of 12 total)
  • Avatar of guitfu
    Justin
    Participant

    @justineterniawebcom

    Anyone have any ideas?

    Avatar of henrywright
    henrywright
    Participant

    @henrywright

    Hi @justineterniawebcom

    I’m trying to understand your requirements – do you just want to send an email (perhaps to an admin) once a user has completed all of their profile fields?

    Avatar of henrywright
    henrywright
    Participant

    @henrywright

    Just to let you know xprofile_updated_profile fires even if there was an error updating the user’s profile.

    I think xprofile_profile_field_data_updated is the hook you need as it fires only on successful update.

    Avatar of guitfu
    Justin
    Participant

    @justineterniawebcom

    Ya all the fields in the main tab (or at least all the required fields).

    For some reason wp_mail is having conflicts with our site so we may need to just update a custom user meta field instead. That’s what the code above is attempting to do, but it doesn’t seem to add the meta key. If that can work, we can keep track of who is updated by who has that meta key.

    xprofile_profile_field_data_updated is helpful – thank you!

    Seems like xprofile_updated_profile wasn’t firing at all, whether on successful or unsuccessful attempts. Or at least it wasn’t updating the meta key in the first code snippet above.

    Avatar of guitfu
    Justin
    Participant

    @justineterniawebcom

    So to summarize, we need:

    1. To hook into xprofile_profile_field_data_updated
    2. We need the hooked function to check for empty profile fields in the main tab
    3. If there are no empty fields, it needs to notify the admin – via updating a custom meta key and sending the admin an email, preferably. But we can settle for just the meta key updated.
    Avatar of shanebpdev
    shanebp
    Participant

    @shanebp

    Use something like this:

    http://wordpress.org/plugins/buddypress-profile-progression/

    Omit the visual and add the member to your admin display and / or send an email when ‘points == x’

    Avatar of guitfu
    Justin
    Participant

    @justineterniawebcom

    That’s a great idea @shanebp! I looked at that plugin before but didn’t dive into the code much.

    It looks like there’s a function bppp_get_user_progression_percent() that can be used.

    Should this work?

    function my_update_profile_mark() {
    $percentage = echo bppp_get_user_progression_percent();
    
    if ($percentage == 100 && !current_user_can('edit_posts')) {
       $user_ID = get_current_user_id();
       $power_level = 9000;
       add_user_meta( $user_ID, '_level_of_power', $power_level);
       }
    }
    add_action( ‘xprofile_profile_field_data_updated’, ‘my_update_profile_mark’ );

    Again, I’m pretty new to PHP so I might be missing something here. I added the user capability check because editing/creating posts is a capability they earn after completing their profile. So it checks to see if they’ve already been promoted.

    Avatar of henrywright
    henrywright
    Participant

    @henrywright

    Assuming bppp_get_user_progression_percent returns the percentage completed then your function looks spot on.

    The conditional if ( $percentage == 100 && ! current_user_can( 'edit_posts' ) ) will ensure the user’s meta is updated only if:

    $percentage == 100 – all profile fields are completed
    ! current_user_can( 'edit_posts' ) – they’ve not already reached 100% profile completion at some point

    Avatar of guitfu
    Justin
    Participant

    @justineterniawebcom

    That’s great @henrywright! Thank you! I will try it out. It seems like if this works, then my first snippet in the original post should’ve at least updated the meta when I was testing it. But hopefully this will be different!

    Avatar of guitfu
    Justin
    Participant

    @justineterniawebcom

    It’s not working :( I even tested it as simple as possible. (The original first snippet above with no conditionals but with ‘add_action( ‘xprofile_profile_field_data_updated’, ‘my_update_profile_mark’ );’ instead. It seems like ‘xprofile_profile_field_data_updated’ isn’t firing. Any way ideas what I’m doing wrong?

    Avatar of henrywright
    henrywright
    Participant

    @henrywright

    xprofile_screen_edit_profile

    Try hooking to that. It fires every time a user’s profile is updated via the profile edit screen. Your conditional will ensure the user meta is added only when appropriate.

    Also, you could also try using update_user_meta instead of add_user_meta

    Avatar of guitfu
    Justin
    Participant

    @justineterniawebcom

    I couldn’t get any of those hooks to work. I ended up editing the members.php file in the profile progression plugin and that worked. Obviously not ideal for updating plugins but it’s working!

    I also was then able to hide the progression bar from users who didn’t own that profile and hide it altogether once it was complete. Those features weren’t part of the original plugin.

    Here’s the code if anybody wants it:

    if (bp_is_my_profile()) {
    
    
        $percent = bppp_get_user_progression_percent();
    
        if ($percent == 100 && !current_user_can('edit_posts')) {
                    my_update_profile_mark();
        }
    
        if ($percent == 100) {
           echo '<div class="bppp-stat-no-display">';
    
        } else {
            echo '<div class="bppp-stat">';
        }
    
    
                echo '<span class="bppp-stat-title">';
                bppp_title();
                echo '</span> ';
                
                echo '<div class="bppp-stat-block">';
                        echo '<div class="bppp-bar">';
                echo '<div class="bppp-bar-mask" style="width:';
                echo (int)(100-bppp_get_user_progression_percent());
                echo '%"></div>';
                echo '</div>';
                        
                echo '</div>';
                echo '<span class="bppp-stat-percent">';
                echo bppp_get_user_progression_percent();
                echo '%</span>';
                echo '</div>';
    
    }
    

    Here’s the function that’s referenced that I added to functions.php:

    function my_update_profile_mark() {
    
       $user_ID = get_current_user_id();
       $power_level = 9001;
       update_user_meta( $user_ID, '_power_level', $power_level);
       
    }

    Oh and of course this got added to style.css:

    .bppp-stat-no-display {
        display: none;
    }
    

    Now I just need to build a custom menu in the backend that queries users based on their role level and the custom user meta key that’s updated here. Hopefully it’s a smoother process!

    Thanks all for your help!

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

You must be logged in to reply to this topic.