Forked from mgmartel/bp-sort-members-by-last-name.php
Last active
December 17, 2015 18:59
-
-
Save MrUzu/5657638 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 | |
// Sort BuddyPress users by last name | |
// | |
// Use this Gist to sort members alphabetically by last name in the | |
// members directory. Follow the instructions below and drop the code | |
// in your functions.php or your functions plugin. | |
// | |
// Updated to work with BP 1.7. See earlier revisions of this gist for | |
// earlier versions of BP. | |
/** | |
* Sort users by last name | |
* | |
* Changes the querystring for the member directory to sort users by their last name | |
* | |
* @param BP_User_Query $bp_user_query | |
*/ | |
function alphabetize_by_last_name( $bp_user_query ) { | |
if ( 'alphabetical' == $bp_user_query->query_vars['type'] ) | |
$bp_user_query->uid_clauses['orderby'] = "ORDER BY substring_index(u.value, ' ', -1)"; | |
} | |
add_action ( 'bp_pre_user_query', 'alphabetize_by_last_name' ); | |
// To reflect the alphabetical sorting of the member directory, you | |
// could also format the names to show the last name first. Here is | |
// a helper function to format names as follows: Last, First Middle | |
// Etc. | |
// | |
// To use this function every time a user's name is shown or queried, | |
// simply add it as a filter: | |
// | |
// `add_filter ('bp_get_member_name', 'format_last_name_first' );` | |
// | |
// To only reformat the name in the member directory, change your | |
// members/members-loop.php file in your (child)theme. Look for | |
// `bp_member_name()` | |
// and replace it with | |
// `echo format_last_name_first ( bp_get_member_name() );` | |
/** | |
* Helper function for formatting a name: Last, First Middle Etc. | |
* | |
* @param string $name | |
* @return string Formatted name | |
*/ | |
function format_last_name_first( $name ) { | |
if ( $first_space = strrpos ( $name, " ") ) | |
$name = substr( $name, $first_space + 1 ) . ", " . substr ( $name, 0, $first_space ); | |
return $name; | |
} | |
// BONUS: To make alphabetical sorting the default sorting, use the | |
// function below. | |
// | |
// In order for this to work properly, `<option value="alphabetical">` | |
// must be the first option for the members-order-select in | |
// members/index.php. | |
function sort_members_alphabetically( $qs = '', $object = false ) { | |
if( $object != 'members' ) //for members only | |
return $qs; | |
if ( empty ( $qs ) || ! strpos ( $qs, "type=" ) ) | |
$qs .= "type=alphabetical&action=alphabetical&page=1"; | |
return $qs; | |
} | |
add_action( 'bp_ajax_querystring', 'sort_members_alphabetically', 11, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very cool