For Developers: Checks & Functions
Please note that this document is a developer reference provided as a courtesy, as our support policy does not include customizations. This reference lists helpful functions and where to find them in the plugin. The plugin’s inline documentation has details on parameters / returned values, as this is meant to be a directory.
WooCommerce Memberships provides several methods for checking data on content restriction and user memberships.
This reference includes helpful conditional checks and global functions for Memberships. Conditional checks return bool true or false. For example:
if ( wc_memberships_is_product_viewing_restricted() ) { echo 'MARCO!'; } else { echo 'POLO!'; }This would check if viewing the current product is restricted to members or not so we can print a different message on the product page. We can also pass in a product id instead to check for a particular product.
Conditional Checks: Products
↑ Back to topAll product conditional checks are located in: /includes/wc-memberships-template-functions.php
wc_memberships_is_product_viewing_restricted()
- @since
- 1.0.0
- @param
- int|null $post_id Optional, defaults to current post
- @return
- bool true if viewing is restricted
wc_memberships_is_product_purchasing_restricted()
- @since
- 1.0.0
- @param
- int|null $post_id Optional, defaults to current post
- @return
- bool true if purchasing is restricted
wc_memberships_product_has_member_discount()
- @since
- 1.0.0
- @param
- int|null $product_id Product ID: optional, defaults to current product
- @return
- bool true if product has discount
wc_memberships_user_has_member_discount()
- @since
- 1.0.0
- @param
- int|null $product_id Product ID: optional, defaults to current product
- @return
- bool true if current user has discount
Conditional Checks: Content
↑ Back to topwc_memberships_is_post_content_restricted()
- @since
- 1.0.0
- @param
- int|null $post_id Optional, defaults to current post
- @return
- bool true if content is restricted
wc_memberships_user_can( $user_id, $action, $target, $when = ” )
- @since
- 1.4.0
- @param
- int $user_id user to check if has access
- @param
- string|array type of capabilities: ‘view’, ‘purchase’ (products only)
- @param
- array $target associative array of content type and content id to access to
- @param
- int|string UTC timestamp to compare for content access (optional, defaults to now)
- @return
- bool true if viewing is permitted
Conditional Checks: Members
↑ Back to topLooking to check for any active membership? This tutorial may be helpful.
wc_memberships_is_user_member()
- @since
- 1.0.0
- @param
- int $user_id Optional, defaults to current user
- @param
- int|string $membership_plan Membership Plan slug, post object or related post ID
- @return
- bool true if user is a member of the plan
wc_memberships_is_user_active_member()
- @since
- 1.0.0
- @param
- int $user_id Optional, defaults to current user
- @param
- int|string $membership_plan Membership Plan slug, post object or related post ID
- @return
- bool true if user is an active member of the plan
Functions: Restricted Content
↑ Back to topwc_memberships_restrict()
- @since
- 1.0.0
- @param
- string $content
- @param
- string|int|array $membership_plans Optional: the membership plan or plans to check against; Accepts a plan slug, id, or an array of slugs or IDs. Default: all plans
- @param
- string $delay
- @param
- bool $exclude_trial
[wcm_restrict]
shortcode for an example.
wc_memberships_get_user_access_time
- @since
- 1.4.0
- @param
- int $user_id User to get access time for
- @param
- array $target Associative array of content type and content id to access to
- @param
- string $action Type of access, ‘view’ or ‘purchase’ (products only)
- @param
- bool $gmt Whether to return a UTC timestamp (default false, uses site timezone)
- @return
- int|null Timestamp of start access time
'post' => id
or 'product' => id
Functions: Membership Plans
↑ Back to topwc_memberships_get_membership_plan()
- @since
- 1.0.0
- @param
- int|string|\WC_Memberships_Membership_Plan $membership_plan Post object, ID or slug of the membership plan
- @return
- \WC_Memberships_Membership_Plan|false Returns the plan object or false on failure
wc_memberships_get_membership_plans()
- @since
- 1.0.0
- @param
- array $args Optional array of arguments, same as for get_posts()
- @return
- \WC_Memberships_Membership_Plan[]
Functions: User Memberships
↑ Back to topLooking to check for any active membership? This tutorial may be helpful.
wc_memberships_get_user_active_memberships()
- @since
- 1.7.0
- @param
- int $user_id WP user ID, defaults to current user
$memberships = wc_memberships_get_user_active_memberships( $user_id ); if ( ! empty( $memberships ) ) { // do something for this active member }
wc_memberships_get_user_membership()
- @since
- 1.0.0
- @param
- mixed $id Optional. Post object or post ID of the user membership, or user ID
- @param
- mixed $plan Optional. Membership Plan slug, post object or related post ID
- @return
- \WC_Memberships_User_Membership|false The User Membership or false if not found
wc_memberships_get_user_memberships()
- @since
- 1.0.0
- @param
- int $user_id Optional, defaults to current user
- @param
- array $args Optional arguments
- @return
- \WC_Memberships_User_Membership[]|null array of user memberships
// get all active memberships for a user; // returns an array of active user membership objects // or null if no memberships are found $user_id = get_current_user_id(); $args = array( 'status' => array( 'active', 'complimentary', 'pending' ), ); $active_memberships = wc_memberships_get_user_memberships( $user_id, $args );
wc_memberships_create_user_membership()
- @since
- 1.3.0
- @param
- array $args Array of arguments {
- @type int|string $user_membership_id membership / post ID for the membership (used when renewing)
- @type int|string $plan_id membership plan / post object ID
- @type int|string $user_id user ID for the membership
- @type int|string $product_id the ID for the access-granting product
- @type int|string $order_id the order in which access is granted }
- @param
- string $action Action – either ‘create’ or ‘renew’ — when in doubt, use ‘create’
- @return
- \WC_Memberships_User_Membership|\WP_Error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Automatically grant membership to a plan at registration | |
function sv_add_membership_at_registration( $user_id ) { | |
if ( ! function_exists( 'wc_memberships' ) ) { | |
return; | |
} | |
$args = array( | |
// Enter the ID (post ID) of the plan to grant at registration | |
'plan_id' => 253, | |
'user_id' => $user_id, | |
); | |
wc_memberships_create_user_membership( $args ); | |
} | |
add_action( 'user_register', 'sv_add_membership_at_registration', 15 ); |
Functions: Other Functions
↑ Back to topwc_memberships_get_memberships_from_subscription()
- @since
- 1.6.0
- @param
- string|int|\WC_Subscription $subscription A Subscription id, object (or key for Subscriptions
- @return
- \WC_Memberships_User_Membership[] An array of User Membership objects or empty array if none found
wc_memberships_get_member_product_discount()
- @since
- 1.4.0
- @param
- \WC_Memberships_User_Membership $user_membership The user membership object
- @param
- int|\WC_Product $product The product object or id to get discount for
- @return
- string discount value
wc_memberships_get_members_area_url()
- @since
- 1.4.0
- @param
- int|\WC_Memberships_Membership_Plan $membership_plan Object or id
- @param
- string $member_area_section Optional, which section of the member area to point to
- @param
- int|string $paged Optional, for paged sections
- @return
- string Unescaped URL
wc_memberships_get_members_area_sections()
- @since
- 1.4.0
- @param
- int|string $membership_plan Optional: membership plan id for filtering purposes
- @return
- array of sections