Created
July 5, 2023 14:08
-
-
Save JarrydLong/7b64606c9cedddd4b7d52d896d2f1e41 to your computer and use it in GitHub Desktop.
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 //do not copy | |
/** | |
* This recipe adds a sortable column to the member list. | |
* | |
* This assumes that a 'country' User Field has been created. | |
* | |
* 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 mypmpro_custom_sortable_memberlist_col( $columns ) { | |
$columns["pmpro_bcountry"] = "Country"; | |
return $columns; | |
} | |
add_filter('manage_memberships_page_pmpro-memberslist_columns', 'mypmpro_custom_sortable_memberlist_col'); | |
add_filter('manage_memberships_page_pmpro-memberslist_sortable_columns', 'mypmpro_custom_sortable_memberlist_col'); | |
function mypmpro_country_column( $colname, $user_id ) { | |
if ( $colname == 'pmpro_bcountry'){ | |
$country = get_user_meta( $user_id, 'pmpro_bcountry', true ); | |
if( empty( $country ) ) { | |
echo " - "; | |
} else { | |
echo $country; | |
} | |
} | |
} | |
add_action('pmpro_manage_memberslist_custom_column', 'mypmpro_country_column', 10, 2); | |
function mypmpro_sortable_country_col( $query ) { | |
global $wpdb; | |
if( ! empty( $_REQUEST['only'] ) ) { | |
$country = "AND umh.meta_value = '".sanitize_text_field( $_REQUEST['only'] )."' "; | |
} else { | |
$country = ''; | |
} | |
//Change pmpro_bcountry to the relevant field name | |
$query = str_replace( "WHERE mu.membership_id > 0 AND mu.status = 'active'", "LEFT JOIN $wpdb->usermeta umh ON umh.meta_key = 'pmpro_bcountry' AND u.ID = umh.user_id WHERE mu.membership_id > 0 AND mu.status = 'active' ".$country, $query ); | |
return $query; | |
} | |
add_filter( 'pmpro_members_list_sql', 'mypmpro_sortable_country_col', 10, 1 ); | |
function mypmro_allowed_country_orderby( $allowed ) { | |
$allowed['Country'] = 'umh.meta_value'; | |
return $allowed; | |
} | |
add_filter( 'pmpro_memberslist_allowed_orderbys', 'mypmro_allowed_country_orderby', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a partner recipe to create a dropdown select for countries on the memberslist page - https://gist.github.com/ipokkel/5731b00b3be9c49ba2ee642216b5765c