Description
Sets user access for a given product. (As of plugin version 3.4.2, use wpmem_set_user_membership() instead)
Parameters
$product
(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 product 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 product was successful.
Examples
// Basic usage of how the function is applied: wpmem_set_user_product( $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_product'. * 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_product' ); function my_set_basic_product( $fields ) { // Defaults. $product_meta = 'my_basic_product'; $user_id = $fields['ID']; // Set product access. wpmem_set_user_product( $product_meta, $user_id ); }
Changelog
- Introduced in version 3.2.3
- Added $date argument in version 3.2.6
- As of version 3.4.2, use wpmem_set_user_membership() instead.
Source
wpmem_set_user_product() is located in /includes/api/api-users.php.