Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- Add the following code to your theme’s functions.php file or to a custom plugin.
- php
- Copy code
- // Hook into the LearnDash action when a user is added to a group
- add_action('ld_added_group_access', 'wp_fusion_schedule_user_removal', 10, 2);
- /**
- * Schedule a user to be removed from a LearnDash group after 180 days.
- *
- * @param int $user_id The ID of the user being added to the group.
- * @param int $group_id The ID of the group the user is added to.
- */
- function wp_fusion_schedule_user_removal($user_id, $group_id) {
- // Schedule the removal only if it hasn't been scheduled yet
- if (!wp_next_scheduled('wp_fusion_remove_user_from_group', [$user_id, $group_id])) {
- wp_schedule_single_event(
- time() + (180 * DAY_IN_SECONDS),
- 'wp_fusion_remove_user_from_group',
- [$user_id, $group_id]
- );
- }
- }
- // Custom action to remove the user from the group
- add_action('wp_fusion_remove_user_from_group', 'wp_fusion_execute_user_removal', 10, 2);
- /**
- * Remove a user from a LearnDash group.
- *
- * @param int $user_id The ID of the user to remove.
- * @param int $group_id The ID of the group from which the user will be removed.
- */
- function wp_fusion_execute_user_removal($user_id, $group_id) {
- // Verify user and group IDs are valid
- if (get_user_by('id', $user_id) && get_post_type($group_id) === 'groups') {
- ld_update_group_access($user_id, $group_id, $remove = true);
- }
- }
- Explanation
- ld_added_group_access Action: This LearnDash hook triggers when a user is added to a group.
- wp_schedule_single_event: This schedules an event 180 days later to remove the user from the group.
- wp_fusion_remove_user_from_group: This is the custom action that removes the user from the group after the delay.
- Verification and Removal: The wp_fusion_execute_user_removal function verifies that the user and group exist before removing the user.
- Notes
- This code uses DAY_IN_SECONDS for clarity.
- 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