Description
Sets user access for a given product.
Parameters
$membership
(string) (required) The meta key of the product to set access for.
$user_id
(integer) (optional) The user ID to assign access to. If no user ID is passed, it will set the product for the currently logged in user.
$date
(string|date|boolean) (optional) The user’s expiration date for the membership in either MySQL date format (YYYY-MM-DD 00:00:00) or simply YYYY-MM-DD. Default value is false and the expiration will be set from the current date.
Return Value
boolean (true|false) Returns true if setting the membership was successful.
Examples
// Basic usage of how the function is applied: wpmem_set_user_membership( $product_meta, $user_id, $date ); /* * The practical example below will set a default * membership for a user when they register. In this * example, it will set access for 'my_basic_membership'. * In a real world application, get the slug for the * membership you want to set at registration from the * Membership > All Products table. * * The example uses the 'wpmem_post_register_data' * action to apply the membership at registration. * @see: https://rocketgeek.com/plugins/wp-members/docs/filter-hooks/wpmem_post_register_data/ */ add_action( 'wpmem_post_register_data', 'my_set_basic_membership' ); function my_set_basic_membership( $fields ) { // Defaults. $membership = 'my_basic_membership'; $user_id = $fields['ID']; // Set membership access. wpmem_set_user_membership( $membership, $user_id ); }
Changelog
- Introduced in version 3.4.2
Source
wpmem_set_user_product() is located in /includes/api/api-users.php.