Advertisement
verygoodplugins

Untitled

Nov 3rd, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.28 KB | None | 0 0
  1. Here’s a custom function for LearnDash that will hook into the action when a user is added to any group. It will schedule an event to remove that user from the group 180 days later. This uses WordPress’s scheduling functions to delay the removal.
  2.  
  3. Add the following code to your theme’s functions.php file or to a custom plugin.
  4.  
  5. php
  6. Copy code
  7. // Hook into the LearnDash action when a user is added to a group
  8. add_action('ld_added_group_access', 'wp_fusion_schedule_user_removal', 10, 2);
  9.  
  10. /**
  11.  * Schedule a user to be removed from a LearnDash group after 180 days.
  12.  *
  13.  * @param int $user_id The ID of the user being added to the group.
  14.  * @param int $group_id The ID of the group the user is added to.
  15.  */
  16. function wp_fusion_schedule_user_removal($user_id, $group_id) {
  17.     // Schedule the removal only if it hasn't been scheduled yet
  18.     if (!wp_next_scheduled('wp_fusion_remove_user_from_group', [$user_id, $group_id])) {
  19.         wp_schedule_single_event(
  20.             time() + (180 * DAY_IN_SECONDS),
  21.             'wp_fusion_remove_user_from_group',
  22.             [$user_id, $group_id]
  23.         );
  24.     }
  25. }
  26.  
  27. // Custom action to remove the user from the group
  28. add_action('wp_fusion_remove_user_from_group', 'wp_fusion_execute_user_removal', 10, 2);
  29.  
  30. /**
  31.  * Remove a user from a LearnDash group.
  32.  *
  33.  * @param int $user_id The ID of the user to remove.
  34.  * @param int $group_id The ID of the group from which the user will be removed.
  35.  */
  36. function wp_fusion_execute_user_removal($user_id, $group_id) {
  37.     // Verify user and group IDs are valid
  38.     if (get_user_by('id', $user_id) && get_post_type($group_id) === 'groups') {
  39.         ld_update_group_access($user_id, $group_id, $remove = true);
  40.     }
  41. }
  42. Explanation
  43. ld_added_group_access Action: This LearnDash hook triggers when a user is added to a group.
  44. wp_schedule_single_event: This schedules an event 180 days later to remove the user from the group.
  45. wp_fusion_remove_user_from_group: This is the custom action that removes the user from the group after the delay.
  46. Verification and Removal: The wp_fusion_execute_user_removal function verifies that the user and group exist before removing the user.
  47. Notes
  48. This code uses DAY_IN_SECONDS for clarity.
  49. Be sure to test the function, as it will execute only once for each user and group.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement