Created
November 12, 2020 18:06
-
-
Save ronalfy/2603964ef8126d6cde465d267e7a97b2 to your computer and use it in GitHub Desktop.
PMPro - Bulk Approve Existing Members
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Bulk approve existing members. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
/** | |
* Bulk Approve all users | |
*/ | |
function bulk_approve_all_users() { | |
global $current_user; | |
$bulkApprovalComplete = get_site_option( 'bulk_approval_complete_tz14' ); | |
error_log( "Approving? BAC: $bulkApprovalComplete" ); | |
if ( ! $bulkApprovalComplete ) { | |
// Create the WP_User_Query object | |
$wp_user_query = new WP_User_Query(); | |
// Get the results | |
$users = get_users( array( | |
'number' => 500, | |
) ); | |
$number_of_users = count( $users ); | |
error_log( " - Total records to process: {$number_of_users}" ); | |
// Check for results | |
if ( ! empty( $users ) ) { | |
// loop trough each author | |
foreach ( $users as $user ) { | |
// add meta all the user's data | |
// add_user_meta( $user->id, 'icap-approved', true, true ); // 5 = number of points existing users will get | |
$user_id = $user->ID; | |
$level = pmpro_getMembershipLevelForUser( $user_id ); | |
// get user's current level if none given | |
if ( empty( $level_id ) ) { | |
$user_level = pmpro_getMembershipLevelForUser( $user_id ); | |
$level_id = $user_level->id; | |
} | |
do_action( 'pmpro_approvals_before_approve_member', $user_id , $level_id ); | |
// update user meta to save timestamp and user who approved | |
update_user_meta( | |
$user_id, | |
'pmpro_approval_' . $level_id, | |
array( | |
'status' => 'approved', | |
'timestamp' => current_time( 'timestamp' ), | |
'who' => $current_user->ID, | |
'approver' => 'BULK UPDATE', | |
) | |
); | |
// delete the approval count cache | |
delete_transient( 'pmpro_approvals_approval_count' ); | |
do_action( 'pmpro_approvals_after_approve_member', $$user_id , $level_id ); | |
error_log( "Approving:$user_id" ); | |
} | |
} | |
add_option( 'bulk_approval_complete_tz9', true ); | |
} | |
} | |
add_action( 'init', 'bulk_approve_all_users', 50 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment