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

  • Rupok
    • Support Ninja

    Hi Raman,

    So sorry to hear the inconvenience you are having. As you said “when used through plugin or code in functions.php, it gives an error in chrome”, can you please tell us with which WordPress hook did you hook your code?

    Moreover, can you turn on debug mode in WordPress? To enable it, open your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

    define('WP_DEBUG', true);

    In order to enable the error logging to a file on the server you need to add yet one more similar line:

    define( 'WP_DEBUG_LOG', true );

    In this case the errors will be saved to a debug.log log file inside the /wp-content/directory.

    Depending on whether you want your errors to be only logged or also displayed on the screen you should also have this line there, immediately after the line mentioned above:

    define( 'WP_DEBUG_DISPLAY', false );

    The wp-config.php is located in your WordPress root directory. It’s the same file where the database configuration settings are. You will have to access it by FTP or SFTP in order to edit it.

    Past errors here. If file is very long, paste them to a text file and attach with your reply.

    I’m looking forward to hearing from you and resolving this issue as soon as possible.

    Have a nice day. Cheers!

    Rupok

  • techie2566
    • Design Lord, Child of Thor

    Hi Rupok,

    Thank you for quick reply, the hook I am using is of Gravity Form (please pardon me if I say something wrong, I am new to WordPress coding):

    add_action( ‘gform_after_submission’, ‘premium_member’, 10, 2 );

    Whenever a user registers on gravity form it will run the code mentioned in the original post.

    Thank you,

    Raman

  • Nithin
    • Support Wizard

    Hey techie2566,

    Hope you are doing good today. :slight_smile:

    Could you please try the following code, the mentioned hook “gform_after_submission” won’t register memberships to users:

    <?php

    function custom_add_membership( $user_id ) {

    $membership_id = 57;
    $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();

    }

    add_action( 'user_register', 'custom_add_membership', 10, 1);

    You can add the above code as a mu-plugins, please check this link on how to configure mu-plugins:

    https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    If you still have issue even after that, please share the fatal error what you are seeing so that we could have a better idea regarding this.

    Please do let us know how that goes, so that we could give a closer look, if needed. Have a nice day. :slight_smile:

    Best Regards,

    Nithin

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.