Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Created July 7, 2023 08:55
Show Gist options
  • Save ipokkel/5731b00b3be9c49ba2ee642216b5765c to your computer and use it in GitHub Desktop.
Save ipokkel/5731b00b3be9c49ba2ee642216b5765c to your computer and use it in GitHub Desktop.
<?php
/**
* This recipe adds a dropdown to the Members List page to filter by country.
*
* This requires and extends the recipe to add a sortable column to the member list.
* @link https://gist.github.com/JarrydLong/7b64606c9cedddd4b7d52d896d2f1e41
*
* 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 my_pmpro_add_countries_dropdown_to_memberslist() {
// Let's only do this if required customization recipe is loaded
if ( ! function_exists( 'mypmpro_sortable_country_col' ) ) {
return;
}
// base url
$base_url = admin_url( 'admin.php' ) . '?page=pmpro-memberslist&orderby=Country&order=asc';
// country 2 letter codes
$countries = array(
'us' => 'United States',
'ca' => 'Canada',
'gb' => 'United Kingdom',
'au' => 'Australia',
'de' => 'Germany',
'cz' => 'Czech Republic',
'po' => 'Poland',
);
$only = false;
if ( ! empty( $_REQUEST['only'] ) ) {
$only = sanitize_text_field( $_REQUEST['only'] );
}
?>
<select id="pmpro-memberslist-country-dropdown">
<option value="">All Countries</option>
<?php foreach ( $countries as $code => $country ) { ?>
<option value="<?php echo esc_attr( $code ); ?>" <?php if ( $only == $code ) { ?>selected="selected"<?php } ?>><?php echo esc_html( $country ); ?></option>
<?php } ?>
</select>
<script>
jQuery(document).ready(function($) {
$('#pmpro-memberslist-country-dropdown').change(function() {
window.location.href = '<?php echo $base_url; ?>&only=' + this.value;
});
});
</script>
<?php
}
add_action( 'pmpro_memberslist_before_table', 'my_pmpro_add_countries_dropdown_to_memberslist' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment