Last active
April 6, 2023 13:05
-
-
Save ipokkel/eec456791d9807213f39fcffb748bc8c to your computer and use it in GitHub Desktop.
Example custom directory widget with Membership Levels as a dropdown select.
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 | |
/** | |
* Plugin Name: My PMPro Directory Widget | |
* Description: Add widget to Member Directory page to filter results. | |
*/ | |
class My_PMPro_Directory_Widget extends WP_Widget { | |
/** | |
* Sets up the widget | |
*/ | |
public function __construct() { | |
parent::__construct( | |
'my_pmpro_directory_widget', | |
'My PMPro Directory Widget', | |
array( 'description' => 'Filter the PMPro Member Directory' ) | |
); | |
} | |
/** | |
* Code that runs on the frontend. | |
* | |
* Modify the content in the <li> tags to | |
* create filter inputs in the sidebar | |
*/ | |
public function widget( $args, $instance ) { | |
// If we're not on a page with a PMPro directory, return. | |
global $post; | |
if ( ! is_a( $post, 'WP_Post' ) || ! has_shortcode( $post->post_content, 'pmpro_member_directory' ) ) { | |
return; | |
} | |
?> | |
<aside id="my_pmpro_directory_widget" class="widget my_pmpro_directory_widget"> | |
<h3 class="widget-title">Filter Directory</h3> | |
<form> | |
<ul> | |
<li> | |
<strong>Membership Level:</strong><br/> | |
<select name="membership_levels[]"> | |
<option value="">-- Select --</option> | |
<?php | |
global $pmpro_levels; | |
foreach ( $pmpro_levels as $key => $value ) { | |
// Check if this value should default to be selected. | |
$selected_modifier = ''; | |
if ( ! empty( $_REQUEST['membership_levels'] ) && in_array( $key, $_REQUEST['membership_levels'] ) ) { | |
$selected_modifier = 'selected'; | |
} | |
// Add checkbox. | |
echo '<option value="' . esc_html( $key ) . '" ' . esc_html( $selected_modifier ) . '>' . esc_html( $value->name ) . '</option><br/>'; | |
} | |
?> | |
</select> | |
</li> | |
<li> | |
<strong>First Name:</strong><br/> | |
<?php | |
// Check if there was a value passed in. | |
$first_name = ''; | |
if ( ! empty( $_REQUEST['first_name'] ) /* && is_numeric( $_REQUEST['first_name'] ) */ ) { | |
$first_name = ' value=' . $_REQUEST['first_name']; | |
} | |
// Add inputs. | |
echo '<input type="text" name="first_name"' . esc_html( $first_name ) . '><br/>'; | |
?> | |
</li> | |
<li> | |
<strong>Last Name:</strong><br/> | |
<?php | |
// Check if there was a value passed in. | |
$last_name = ''; | |
if ( ! empty( $_REQUEST['last_name'] ) ) { | |
$last_name = ' value=' . $_REQUEST['last_name']; | |
} | |
// Add inputs. | |
echo '<input type="text" name="last_name"' . esc_html( $last_name ) . '><br/>'; | |
?> | |
</li> | |
<li> | |
<strong>Street Name:</strong><br/> | |
<?php | |
// Check if there was a value passed in. | |
$street_name = ''; | |
if ( ! empty( $_REQUEST['street_name'] ) ) { | |
$street_name = ' value=' . $_REQUEST['street_name']; | |
} | |
// Add inputs. | |
echo '<input type="text" name="street_name"' . esc_html( $street_name ) . '><br/>'; | |
?> | |
</li> | |
<li><input type="submit" value="Filter"></li> | |
</ul> | |
</form> | |
</aside> | |
<?php | |
} | |
} | |
/** | |
* Check $_REQUEST for parameters from the widget. Add to SQL query. | |
*/ | |
function my_pmpro_directory_widget_filter_sql_parts( $sql_parts, $levels, $s, $pn, $limit, $start, $end, $order_by, $order ) { | |
global $wpdb; | |
// Filter results based on membership level if a level was selected. | |
if ( ! empty( $_REQUEST['membership_levels'] ) && is_array( $_REQUEST['membership_levels'] ) && is_numeric( $_REQUEST['membership_levels'][0] ) ) { | |
// User's membership level is already joined, so we can skip that step. | |
$sql_parts['WHERE'] .= " AND mu.membership_id in ('" . implode( "','", $_REQUEST['membership_levels'] ) . "') "; | |
} | |
// Filter results based on coat color if a color is selected. | |
if ( ! empty( $_REQUEST['first_name'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_first_name ON um_first_name.meta_key = 'first_name' AND u.ID = um_first_name.user_id "; | |
$sql_parts['WHERE'] .= ' AND um_first_name.meta_value LIKE "%' . $_REQUEST['first_name'] . '%"'; | |
} | |
if ( ! empty( $_REQUEST['last_name'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_last_name ON um_last_name.meta_key = 'last_name' AND u.ID = um_last_name.user_id "; | |
$sql_parts['WHERE'] .= ' AND um_last_name.meta_value LIKE "%' . $_REQUEST['last_name'] . '%"'; | |
} | |
if ( ! empty( $_REQUEST['street_name'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_pri_st_name ON um_pri_st_name.meta_key = 'pri_st_name' AND u.ID = um_pri_st_name.user_id "; | |
$sql_parts['WHERE'] .= ' AND um_pri_st_name.meta_value LIKE "%' . $_REQUEST['street_name'] . '%"'; | |
} | |
return $sql_parts; | |
} | |
add_filter( 'pmpro_member_directory_sql_parts', 'my_pmpro_directory_widget_filter_sql_parts', 10, 9 ); | |
/** | |
* Registers widget. | |
*/ | |
function my_pmpro_register_directory_widget() { | |
register_widget( 'My_PMPro_Directory_Widget' ); | |
} | |
add_action( 'widgets_init', 'my_pmpro_register_directory_widget' ); | |
/** | |
* Remember filters being used while using "Next" and "Previous" buttons. | |
* | |
* @since 2020/06/25 | |
*/ | |
function my_pmpromd_pagination_url_filter_directory( $query_args ) { | |
$directory_filters = array( 'membership_levels', 'first_name', 'last_name', 'street_name' ); | |
foreach ( $directory_filters as $directory_filter ) { | |
if ( ! empty( $_REQUEST[ $directory_filter ] ) ) { | |
$query_args[ $directory_filter ] = $_REQUEST[ $directory_filter ]; | |
} | |
} | |
return $query_args; | |
} | |
add_filter( 'pmpromd_pagination_url', 'my_pmpromd_pagination_url_filter_directory' ); |
Hi @JonMSailer , if you're dropdown values are in an array format you would need to customize the SQL query, for further details you may refer to this guide:
https://www.paidmembershipspro.com/allow-users-to-filter-results-in-your-member-directory/
Here is an example custom SQL query for PMPro Directory Widget that also filters field values stored as arrays
https://gist.github.com/ipokkel/cd204e38853aac1c072a4ac2f559e3f9
I hope this helps get you or your developer started. :)
Thank you!
Factor 360, Inc
JON M. SAILER
WEB DEVELOPER
***@***.***> ***@***.***
<http://www.factor360.com/> www.factor360.com
605.280.1897
CONFIDENTIALITY NOTICE - This email is intended only for the person(s) named in the message header. Unless otherwise indicated, it contains information that is confidential, privileged and/or exempt from disclosure under applicable law. If you have received this message in error, please notify the sender of the error and delete the message.
From: Theuns Coetzee ***@***.***>
Sent: Thursday, April 6, 2023 4:02 AM
To: ipokkel ***@***.***>
Cc: Mention ***@***.***>
Subject: Re: ipokkel/example-custom-directory-widget.php
@ipokkel commented on this gist.
…_____
Hi @JonMSailer <https://github.com/JonMSailer> , if you're dropdown values are in an array format you would need to customize the SQL query, for further details you may refer to this guide:
https://www.paidmembershipspro.com/allow-users-to-filter-results-in-your-member-directory/
Here is an example custom SQL query for PMPro Directory Widget that also filters field values stored as arrays
https://gist.github.com/ipokkel/cd204e38853aac1c072a4ac2f559e3f9
I hope this helps get you or your developer started. :)
—
Reply to this email directly, view it on GitHub <https://gist.github.com/ipokkel/eec456791d9807213f39fcffb748bc8c#gistcomment-4528360> or unsubscribe <https://github.com/notifications/unsubscribe-auth/A64ZPSNGBCKPZS3G2RCPM7DW72ICRBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTANRVGEZTQNZQU52HE2LHM5SXFJTDOJSWC5DF> .
You are receiving this email because you were mentioned.
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub> .
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Theuns. I am trying to setup a dropdown for another field that I setup in the "Manage User Fields". I tried to just change the $pmpro_levels to one of my fields but it just won't populate the dropdown menu...Do you have an example of another dropdown option that maybe pulls in a list of States or something?