Membership Pro 2 - Change of hook ms_model_relationship_create_ms_relationship_before?

Hi there,

On Sign Up we have been using the function below to assign the respective Membership (Membership 2 Pro Version 1.0.0.8). Yet, while testing the latest Version 1.0.2.1. on staging this doesnt seem to work anymore.

Has the hook changed?

Thanks for any insight!

FUNCTION:

add_action( 'ms_model_relationship_create_ms_relationship_before', 'set_membership_level', 10, 5 );

function set_membership_level(
$membership_id,
$user_id,
$gateway_id,
$move_from_id
) {
$current_user = get_user_by ('id', $user_id);
if ($membership_id == 'xxx') {
$member_type = bp_set_member_type( $user_id, 'community' );
$current_user->set_role('subscriber');
} else if ($membership_id == 'yyy') {
$member_type = bp_set_member_type( $user_id, 'professional' );
$current_user->set_role('professional');
}
}

  • Philipp Stracker

    Hi Melanie,

    the hook still exists and did not change since 1.0.0.8

    However, I notice you call it with the last param being "5" - the correct value should be "4", like this:
    add_action( 'ms_model_relationship_create_ms_relationship_before', 'set_membership_level', 10, 4 );

    If you want to test if the hook is called and what parameter are passed to it you can change your function to somthing like this:

    function set_membership_level($membership_id,$user_id,$gateway_id,$move_from_id) {
      $current_user = get_user_by ( 'id', $user_id );
      lib2()->debug->dump( $membership_id, $user_id, $gateway_id, $move_from_id, $current_user );
      wp_die();

    Of course only do this on a test site and not on your productive site where new users might register any time... With this change you will see some debug output when a new subscription is created.

    You also might to check your servers error-log file; if there is a problem in the current hook call you possibly find additional details in the error logs.

    -----

    Tipp: If you only want to assign the role when a user has an ACTIVE subscription you can use this code instead:

    // Add the role when subscription becomes active (i.e. after payment)
    add_action( 'ms_subscription_status-active', 'set_membership_level', 10, 3 );
    function set_membership_level( $subscription, $membership, $member ) {
    	$current_user = get_user_by( 'id', $subscription->user_id );
    	if ( $membership->id == 'xxx' ) {
    		// Existing code here...
    	}
    }
    
    // Remove the role again on expiration/deactivation.
    add_action( 'ms_subscription_status-expired', 'reset_membership_level', 10, 3 );
    add_action( 'ms_subscription_status-deactivated', 'reset_membership_level', 10, 3 );
    function reset_membership_level( $subscription, $membership, $member ) {
    	$current_user = get_user_by( 'id', $subscription->user_id );
    	if ( $membership->id == 'xxx' ) {
    		// Remove the role again...
    	}
    }

    I hope this helps. Let us know if you need more help with this :wink:
    Thanks, Philipp

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.