Skip to content

Instantly share code, notes, and snippets.

@verygoodplugins
Last active August 4, 2021 10:26
Show Gist options
  • Save verygoodplugins/8172272e337b58974ef3bbc64a55e46d to your computer and use it in GitHub Desktop.
Save verygoodplugins/8172272e337b58974ef3bbc64a55e46d to your computer and use it in GitHub Desktop.
<?php
// Deletes WordPress user when the tag "REMOVE USER" is applied.
// Works when a webhook is received or during a batch Resync Tags operation is run.
// USE WITH CAUTION.
function my_wpf_delete_user( $user_id, $user_tags ) {
if ( wp_fusion()->user->has_tag( 'REMOVE USER', $user_id ) && ! user_can( $user_id, 'manage_options' ) ) {
// If this is a webhook WP_User might not be loaded yet
require_once( ABSPATH . 'wp-admin/includes/user.php' );
wp_delete_user( $user_id );
wpf_log( 'notice', $user_id, 'User ID ' . $user_id . ' was deleted by the my_wpf_delete_user() function.' );
// Don't run any other actions on the user, since they're deleted now
remove_all_actions( 'wpf_tags_modified' );
// Add this action back so a batch Resync Tags can delete more users
add_action( 'wpf_tags_modified', 'my_wpf_delete_user', 1, 2 );
}
}
add_action( 'wpf_tags_modified', 'my_wpf_delete_user', 1, 2 );
@verygoodplugins
Copy link
Author

@rusuandreirobert. Good catch. For some reason I thought 2 meant remove all actions with a priority higher than 2. Not sure where I got that from as that's clearly not true 🙃

The idea was to let this function continue to run during a batch Resync Tags (on multiple users), in case multiple users needed to be deleted.

Updated the gist. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment