Title: assign role
Last modified: June 4, 2018

---

# assign role

 *  Resolved [wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * (@wpbetsy)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/)
 * Can make it assign role automatically according to purchased certain product?
 * That means membership by role.

Viewing 15 replies - 1 through 15 (of 29 total)

1 [2](https://wordpress.org/support/topic/assign-role/page/2/?output_format=md) 
[→](https://wordpress.org/support/topic/assign-role/page/2/?output_format=md)

 *  [markhf](https://wordpress.org/support/users/markhf/)
 * (@markhf)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10362887)
 * Hi [@wpbetsy](https://wordpress.org/support/users/wpbetsy/),
 * Currently the plugin is not doing that, however you can do that with help of 
   any of below filters in the plugin..
 *     ```
               $new_membership_data = apply_filters('hf_memberships_new_membership_data', $new_membership_data, array(
                   'user_id' => (int) $args['user_id'],
                   'product_id' => (int) $args['product_id'],
                   'order_id' => (int) $args['order_id'],
                       ));
       ```
   
 *     ```
               do_action('hf_memberships_user_membership_created', $membership_plan, array(
                   'user_id' => $args['user_id'],
                   'user_membership_id' => $user_membership->get_id(),
                   'is_update' => $updating,
               ));
       ```
   
 *  Thread Starter [wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * (@wpbetsy)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10365516)
 * Where should I put this on? Only put any one of the above? Are there any differences?
 * Do I need to change anything or just place the above code exactly?
 *  [markhf](https://wordpress.org/support/users/markhf/)
 * (@markhf)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10365624)
 * Hi [@wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * Please contact us via [support](https://www.webtoffee.com/support/) to help you
   fix the same.
 *  Thread Starter [wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * (@wpbetsy)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10369556)
 * Hi,
 * In order to save time, could you please just tell the path to the file and location
   that I should put on the file? Or it can be done at admin, just simply tell then.
 * Thank you!
 *  Thread Starter [wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * (@wpbetsy)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10369565)
 * This should be a common question that other users may want to know too.
 *  [markhf](https://wordpress.org/support/users/markhf/)
 * (@markhf)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10369582)
 * Hi [@wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * You can prepare a code snippet using the filter “hf_memberships_new_membership_data”
   as we are exposing below details while creating a membership.
 *     ```
        $new_membership_data = apply_filters('hf_memberships_new_membership_data', 
                   $new_membership_data, array(
                   'user_id' => (int) $args['user_id'],
                   'product_id' => (int) $args['product_id'],
                   'order_id' => (int) $args['order_id'],
                       ));
       ```
   
 * You can put the code snippet in your active theme functions.php
 *  Thread Starter [wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * (@wpbetsy)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10371557)
 * Can you update the plugin? make membership by role?
 *  Thread Starter [wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * (@wpbetsy)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10371560)
 * as a choice in next version?
 *  [markhf](https://wordpress.org/support/users/markhf/)
 * (@markhf)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10371658)
 * Hi [@wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * Currently we have no plan for this to include in the plugin. This can be achieved
   with the help of above filter as you can change to any role while customer joining
   to any membership plan.
 *  Thread Starter [wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * (@wpbetsy)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10374233)
 * Hi,
 * Actually, I want both. By purchasing certain product, the user will be assigned
   a membership (the existing membership plan in your plugin) and a role.
 * How to achieve it easily? I am not familiar with codes.
 *  [markhf](https://wordpress.org/support/users/markhf/)
 * (@markhf)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10375763)
 * Hello [@wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * Please try below code snippet with necessary changes.
 *     ```
       add_filter('hf_memberships_new_membership_data', 'assign_role_after_membership');
   
       function assign_role_after_membership($new_membership_data, $user_id_product_id_order_id) {
   
           $theUser = new WP_User($user_id_product_id_order_id['user_id']);
   
           $role = 'mycustomrole'; // string $role = Role name 
   
           $theUser->add_role($role);
   
           return $new_membership_data;
       }
       ```
   
 *  Thread Starter [wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * (@wpbetsy)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10378003)
 * Which row of the function.php should it be placed? What’s the path to locate 
   the function.php on server?
 * After adding the code, related function will appear at wordpress backend?
 *  [markhf](https://wordpress.org/support/users/markhf/)
 * (@markhf)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10378024)
 * Hi [@wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * Please contact us here in our [support page](https://www.webtoffee.com/product/woocommerce-subscriptions/)
   to help you fix the code snippet in your site.
 *  Thread Starter [wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * (@wpbetsy)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10381259)
 * Hi,
 * Can you please just simply help here? I don’t want to open so many accounts which
   result in chaos.
 * Which row of the function.php should it be placed? What’s the path to locate 
   the function.php on server?
 * After adding the code, related function will appear at wordpress backend?
 *  [markhf](https://wordpress.org/support/users/markhf/)
 * (@markhf)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/#post-10381836)
 * Hi [@wpbetsy](https://wordpress.org/support/users/wpbetsy/)
 * The code snippet can be added to last line of the functions.php just before the
   tag ” ?> ”
 * You can see the functions.php file in the root folder of your active theme ( 
   in child theme if any child theme )
 * By adding the code snippet, each member will be added to the custom role you 
   defined in the code snippet.

Viewing 15 replies - 1 through 15 (of 29 total)

1 [2](https://wordpress.org/support/topic/assign-role/page/2/?output_format=md) 
[→](https://wordpress.org/support/topic/assign-role/page/2/?output_format=md)

The topic ‘assign role’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/xa-woocommerce-memberships_1a5eb7.
   svg)
 * [Memberships for WooCommerce](https://wordpress.org/plugins/xa-woocommerce-memberships/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/xa-woocommerce-memberships/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/xa-woocommerce-memberships/)
 * [Active Topics](https://wordpress.org/support/plugin/xa-woocommerce-memberships/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/xa-woocommerce-memberships/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/xa-woocommerce-memberships/reviews/)

 * 29 replies
 * 3 participants
 * Last reply from: [yaelduckwen](https://wordpress.org/support/users/yaelduckwen/)
 * Last activity: [7 years, 11 months ago](https://wordpress.org/support/topic/assign-role/page/2/#post-10412705)
 * Status: resolved