Unable to add membership through Membership 2 Pro using PHP.

Hi,

I am trying to register a user as a Premium Member, I have tried a code on a page and through creating a plugin and the code works on the page and properly adds a membership to a specific user, but when used through plugin or code in functions.php, it gives an error in chrome (Chrome is currently unable to handle this request. HTTP ERROR 500) and a blank page in firefox.

I tried to debug and found that this line is causing the error:

$member->save();

But without it, I am not able to add a subscription on a particular user, their access remains as a visitor.

Code:

if(is_user_logged_in())

{

$user_info = wp_get_current_user();

$user_id=$user_info->ID;

}

$membership_id = 1234;

$member = MS_Factory::load( ‘MS_Model_Member’, $user_id );

$subscription = $member->add_membership( $membership_id );

if ( $member->has_membership() ) {

$member->is_member = true;

} else {

$member->is_member = false;

}

$member->save();

Thank you,

– Raman