Retrieving the membership record(s) for a given user ID
When working with users and memberships, there are three components:
- 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.
- 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.
- 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(); }