Update membership level with pmpro and wp-discourse

I have used discourse and pmpro both pluginan also zappier addon of pmpro.I m trying to update membership level of my user to discourse.
I am expecting when i change membership level using wp-admin of any user it must be updated at my discource site admin user section.
so what di i need to done this ?? give me some idea about it

1 Like

You’ll need to add an action to your functions.php

/* see example at http://hookr.io/actions/pmpro_after_change_membership_level/ */
do_action( 'pmpro_after_change_membership_level', $level_id, $user_id, $cancel_level );
function action_pmpro_after_change_membership_level( $level_id, $user_id, $cancel_level ) {

 . . . . stuff that gets the membership level and group and adds them to the group

}
2 Likes

can u please explain how can i get up $level_id, $user_id, $cancel_level and group name for updated user

I linked to the documentation. I believe that they get passed by the action hook.

1 Like

hi jay thanks for your reply it worked for me but old value is still remaining there.it means new membership level and old membership level i got both values there so any idea to remove old value of membership level
i am using bellow thing

DiscourseUtilities::remove_user_from_discourse_group( $user_id, $group_name );

this is my remove function

function dcpmp_remove_member_from_group( $level_id, $user_id, $cancel_level ) {
    if ( ! empty( $cancel_level ) ) {
        $group_name = dcpmp_get_level_for_id( $cancel_level );
        if ( is_wp_error( $group_name ) ) {

            return null;
        }

        // Removes the user.
        $result = DiscourseUtilities::remove_user_from_discourse_group( $user_id, $group_name );
        if ( ! empty( $result->success ) ) {
           // Remove the membership level metadata key.
            delete_user_meta( $user_id, "dcpmp_group_{$group_name}" );
        }

        return $result;
    }

    return null;
}
1 Like

I think that there’s a remove from group function in wp-discourse that you can call.

this is the same function used by [Simon Cossar] (https://meta.discourse.org/u/Simon_Cossar)team see this link

Manage group membership in Discourse with WP Discourse SSO

ihave used the same function please need help .i have posted code also see above message
This is my update membershiplevel code
function action_pmpro_after_change_membership_level( $level_id, $user_id, $cancel_level ) {

	$group_name = dcpmp_get_level_for_id( $level_id );
	if ( is_wp_error( $group_name ) ) {

		return null;
	}
	$groupname = DiscourseUtilities::get_discourse_user( $user_id, false );

	// Removes the user.
    $result = DiscourseUtilities::remove_user_from_discourse_group( $user_id, $groupname );
    if ( ! empty( $result->success ) ) {
       // Remove the membership level metadata key.
        delete_user_meta( $user_id, "dcpmp_group_{$groupname}" );
    }
    
    $result1 = DiscourseUtilities::add_user_to_discourse_group( $user_id, $group_name );

    if ( ! empty( $result1->success ) ) {

        // If the user has been added to the group, add a metadata key/value pair that can be used later.
        add_user_meta( $user_id, "dcpmp_group_{$group_name}", 1, true );
    }

return $result;
return $result1;

}
add_action( ‘pmpro_after_change_membership_level’, ‘action_pmpro_after_change_membership_level’, 10, 3 );

I’m not sure what this line in your code is doing:

$groupname = DiscourseUtilities::get_discourse_user( $user_id, false );

That will set $groupname to a Discourse user object. I think you need to remove that line and use the $group_name variable that you are setting in the first line of your code.

1 Like

i have used that before but not working
please provide me solution

If you want us to provide you a solution, you’ll need to pay us. Sign up for a hosting plan at https://discourse.org/buy

2 Likes