Last active
May 16, 2024 18:22
-
-
Save MaximilianoRicoTabo/65c7e5e52d0749bd98306da47802566a to your computer and use it in GitHub Desktop.
Add a level dropdown at the top and bottom of the users table view than add support to bulk add level memberships to users.
This file contains hidden or 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 | |
/** | |
* | |
* Add a level dropdown at the top and bottom of the users table view than add support to bulk | |
* add level memberships to users. Use at your own risk. Users can expect timeouts and rate limiting often | |
* when bulk editing even a relatively small number of members depending on their hosting and how things are wired up. | |
* link: TBD | |
* | |
* 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/ | |
* | |
*/ | |
function pmpro_display_level_selection_for_bulk_users( $which ) { | |
// The attributes need to be different for top and bottom of the table. | |
$id = 'bottom' === $which ? 'pmpro_bulk_change_level2' : 'pmpro_bulk_change_level'; | |
$submit = 'bottom' === $which ? 'pmpro_bulk_change_level_submit2' : 'pmpro_bulk_change_level_submit'; | |
?> | |
<div class="alignleft actions"> | |
<label class="screen-reader-text" for="<?php echo esc_attr( $id ); ?>"> | |
<?php esc_html_e( 'Change level', 'paid-memberships-pro' ); ?> | |
</label> | |
<select id="<?php echo esc_attr( $id ); ?>" name="<?php echo esc_attr( $id ); ?>"> | |
<option value<?php echo esc_attr(""); ?>>-- <?php esc_html_e( 'Choose Level', 'paid-memberships-pro' );?> --</option> | |
<?php | |
$levels = pmpro_getAllLevels(); | |
foreach ( $levels as $level ) { | |
?> | |
<option value="<?php echo esc_attr( $level->id ) ?>"><?php esc_html( $level->name ) ?></option> | |
<?php | |
} | |
?> | |
</select> | |
<input type="submit" name="<?php echo esc_attr( $submit ); ?>" id="<?php echo esc_attr( $submit ); ?>" class="button" value="<?php echo esc_attr( 'Change' ); ?>"> | |
</div> | |
<?php | |
} | |
add_action( 'manage_users_extra_tablenav', 'pmpro_display_level_selection_for_bulk_users', 20, 1 ); | |
/** | |
* Process bulk change level. | |
*/ | |
function pmpro_bulk_change_level() { | |
if( empty( $_GET['pmpro_bulk_change_level'] ) && empty( $_GET['pmpro_bulk_change_level2'] ) ) { | |
return; | |
} | |
if( empty( $_GET['users'] ) ) { | |
return; | |
} | |
$user_ids = array_map( 'sanitize_text_field', ( $_GET['users'] ) ); | |
$level_bottom = sanitize_text_field( $_GET['pmpro_bulk_change_level2'] ); | |
$level_top = sanitize_text_field( $_GET['pmpro_bulk_change_level'] ); | |
$level = $level_bottom !== "-- Choose Level --" ? $level_bottom : $level_top; | |
//loop users and change level | |
foreach( $user_ids as $user_id ) { | |
pmpro_changeMembershipLevel ( $level, $user_id ); | |
} | |
} | |
// Hook into users table screen to process bulk change level. | |
add_action( 'admin_head-users.php', 'pmpro_bulk_change_level' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment