Retrieving The Membership Record(s) For A Given User ID

Note: This is part of the developer docs and is considered custom code.
Unfortunately, we cannot provide support for custom code at this time as we do not have the additional resources that would be necessary to provide support for custom code.

If you need assistance with this, please reach out to our list of consultants for further assistance:
https://codeable.io/developers/restrict-content-pro/

When working with users and memberships, there are three components:

  1. The user account. This is what you're probably most familiar with. The user account is what someone uses to log in to your site. The relevant class here is WP_User.
  2. The customer record. A customer record contains information about the user's presence in Restrict Content Pro. If the user has never purchased a Restrict Content Pro membership then they will not have a customer record. Each customer record is associated with a user account. They're linked by the user ID number. The relevant class here is RCP_Customer.
  3. The membership record. This contains information about the user's membership itself. For now, a customer can only have one membership record, but in the future, a customer may have more than one. Each membership is linked to a customer record using the customer ID number. The relevant class here is RCP_Membership.

With that in mind, here's how you can go from a user ID number to an RCP_Membership object:

$user_id = 123; // User ID number.

// Retrieve the customer record associated with this user ID.
$customer = rcp_get_customer_by_user_id( $user_id );

// If no customer record is found, there will be no memberships.
if ( empty( $customer ) ) {
	return; // Or otherwise exit out of your code.
}

// Once you have the customer object, you can get the customers memberships.
$memberships = $customer->get_memberships();

/*
 * $memberships will be an array. For now it will only have a maximum of one item in it,
 * but this may change in the future so plan your code accordingly.
 */

foreach ( $memberships as $membership ) {
	// $membership will be an RCP_Membership object. You can use it accordingly. Example:
	$status = $membership->get_status();
	$membership_level_id = $membership->get_object_id();
}

Please note: All of these examples serve as a guideline and are here for your convenience. We do not provide support for troubleshooting or modifying these and we do not provide support for any custom development. If you need help with any of these examples, consider hiring a developer.

Powered by Zendesk