Skip to content

Instantly share code, notes, and snippets.

@vapvarun
Created August 22, 2025 05:27
Show Gist options
  • Select an option

  • Save vapvarun/fb2a368c3ace05d2c1c0e5bf60f6d74d to your computer and use it in GitHub Desktop.

Select an option

Save vapvarun/fb2a368c3ace05d2c1c0e5bf60f6d74d to your computer and use it in GitHub Desktop.
BuddyPress Profile Pro - Alphabetical Dropdown Sorting - Automatically sorts dropdown options alphabetically
<?php
/**
* BuddyPress Profile Pro - Alphabetical Dropdown Sorting (Code Snippet)
*
* Add this code to your theme's functions.php file or use a code snippet plugin
* like Code Snippets, WPCode, or Advanced Scripts
*
* This snippet automatically sorts all dropdown, checkbox, radio button, and
* selectize field options alphabetically in BuddyPress Profile Pro
*/
// Initialize the alphabetical sorting
add_action('init', 'wbbpp_initialize_alphabetical_sorting');
function wbbpp_initialize_alphabetical_sorting() {
// Hook into both regular and network options
add_filter('option_wbbpp_profile_fields_settings', 'wbbpp_sort_field_options_alphabetically', 20);
add_filter('site_option_wbbpp_profile_fields_settings', 'wbbpp_sort_field_options_alphabetically', 20);
}
/**
* Sort field options alphabetically
*/
function wbbpp_sort_field_options_alphabetically($settings) {
if (!is_array($settings)) {
return $settings;
}
// Field types to sort
$sortable_types = array('dropdown', 'selectize', 'radio_button', 'checkbox', 'text_dropdown');
// Values that should always appear at the end
$values_at_end = array('Other', 'Autre', 'None', 'N/A', 'Not Applicable', 'Not Specified');
foreach ($settings as $group_key => &$group_fields) {
if (!is_array($group_fields)) {
continue;
}
foreach ($group_fields as $field_key => &$field_info) {
// Check if this field has options to sort
if (!isset($field_info['field_type']['type']) ||
!isset($field_info['field_type']['options']) ||
!is_array($field_info['field_type']['options'])) {
continue;
}
// Check if this field type should be sorted
if (!in_array($field_info['field_type']['type'], $sortable_types)) {
continue;
}
// Get the options
$options = $field_info['field_type']['options'];
$special_values = array();
$regular_options = array();
// Separate special values that should appear at the end
foreach ($options as $option) {
$found_special = false;
foreach ($values_at_end as $end_value) {
if (strcasecmp(trim($option), $end_value) === 0) {
$special_values[] = $option;
$found_special = true;
break;
}
}
if (!$found_special) {
$regular_options[] = $option;
}
}
// Sort regular options alphabetically
natcasesort($regular_options);
// Combine: sorted regular options + special values at end
$field_info['field_type']['options'] = array_values(
array_merge($regular_options, $special_values)
);
}
}
return $settings;
}

BuddyPress Profile Pro - Alphabetical Dropdown Sorting Snippet

🎯 Problem

When you add dropdown options in BuddyPress Profile Pro (like Employee Functions, Departments, Skills), they appear in the order you created them, not alphabetically. Adding new values later means manually rebuilding the entire list to maintain alphabetical order.

βœ… Solution

This code snippet automatically sorts all dropdown, selectize, checkbox, and radio button options alphabetically while keeping special values like "Other" and "None" at the end.

πŸ“‹ Installation Instructions

Method 1: Add to functions.php

// Add this code to your theme's functions.php file
// Path: /wp-content/themes/your-theme/functions.php

Method 2: Using Code Snippets Plugin (Recommended)

  1. Install "Code Snippets" plugin from WordPress repository
  2. Go to Snippets β†’ Add New
  3. Title: "BuddyPress Profile Pro - Sort Dropdowns"
  4. Copy the code below (without the opening <?php tag)
  5. Save and Activate

Method 3: Using WPCode Plugin

  1. Install "WPCode" plugin
  2. Go to Code Snippets β†’ Add Snippet
  3. Choose "Add Your Custom Code (New Snippet)"
  4. Select "PHP Snippet"
  5. Paste the code and activate

πŸ“ The Code

<?php
/**
 * BuddyPress Profile Pro - Alphabetical Dropdown Sorting (Code Snippet)
 * 
 * Add this code to your theme's functions.php file or use a code snippet plugin
 * like Code Snippets, WPCode, or Advanced Scripts
 * 
 * This snippet automatically sorts all dropdown, checkbox, radio button, and 
 * selectize field options alphabetically in BuddyPress Profile Pro
 */

// Initialize the alphabetical sorting
add_action('init', 'wbbpp_initialize_alphabetical_sorting');

function wbbpp_initialize_alphabetical_sorting() {
    // Hook into both regular and network options
    add_filter('option_wbbpp_profile_fields_settings', 'wbbpp_sort_field_options_alphabetically', 20);
    add_filter('site_option_wbbpp_profile_fields_settings', 'wbbpp_sort_field_options_alphabetically', 20);
}

/**
 * Sort field options alphabetically
 */
function wbbpp_sort_field_options_alphabetically($settings) {
    if (!is_array($settings)) {
        return $settings;
    }
    
    // Field types to sort
    $sortable_types = array('dropdown', 'selectize', 'radio_button', 'checkbox', 'text_dropdown');
    
    // Values that should always appear at the end
    $values_at_end = array('Other', 'Autre', 'None', 'N/A', 'Not Applicable', 'Not Specified');
    
    foreach ($settings as $group_key => &$group_fields) {
        if (!is_array($group_fields)) {
            continue;
        }
        
        foreach ($group_fields as $field_key => &$field_info) {
            // Check if this field has options to sort
            if (!isset($field_info['field_type']['type']) || 
                !isset($field_info['field_type']['options']) || 
                !is_array($field_info['field_type']['options'])) {
                continue;
            }
            
            // Check if this field type should be sorted
            if (!in_array($field_info['field_type']['type'], $sortable_types)) {
                continue;
            }
            
            // Get the options
            $options = $field_info['field_type']['options'];
            $special_values = array();
            $regular_options = array();
            
            // Separate special values that should appear at the end
            foreach ($options as $option) {
                $found_special = false;
                foreach ($values_at_end as $end_value) {
                    if (strcasecmp(trim($option), $end_value) === 0) {
                        $special_values[] = $option;
                        $found_special = true;
                        break;
                    }
                }
                if (!$found_special) {
                    $regular_options[] = $option;
                }
            }
            
            // Sort regular options alphabetically
            natcasesort($regular_options);
            
            // Combine: sorted regular options + special values at end
            $field_info['field_type']['options'] = array_values(
                array_merge($regular_options, $special_values)
            );
        }
    }
    
    return $settings;
}

🎨 Examples

Employee Function Dropdown

Before:

Manager
CEO
Developer  
Accountant
Intern
HR Manager
Other

After:

Accountant
CEO
Developer
HR Manager
Intern
Manager
Other (stays at end)

Department Checkboxes

Before:

Sales
HR
IT
Finance
Marketing
None

After:

Finance
HR
IT
Marketing
Sales
None (stays at end)

βš™οΈ Customization

To exclude specific fields from sorting:

// Add this inside the foreach loop, after checking field type
if (in_array($field_key, array('priority_level', 'custom_order'))) {
    continue; // Skip these fields
}

To add more special values that stay at the end:

// Modify the $values_at_end array
$values_at_end = array(
    'Other', 
    'Autre', 
    'None', 
    'N/A', 
    'Not Applicable',
    'Your Value Here' // Add your custom values
);

To sort only specific fields:

// Add this check before sorting
$fields_to_sort = array('job_title', 'department', 'skills');
if (!in_array($field_key, $fields_to_sort)) {
    continue;
}

πŸ” Finding Your Field Names

  1. Go to WordPress Admin β†’ BuddyPress Profile Pro β†’ Fields Settings
  2. Check your field settings
  3. Common field names:
    • job_title
    • employee_function
    • department
    • skills
    • country
    • languages

✨ Features

  • βœ… Sorts dropdown, selectize, radio, and checkbox options
  • βœ… Case-insensitive natural sorting
  • βœ… Keeps "Other", "None", etc. at the end
  • βœ… Works with multisite
  • βœ… No database changes
  • βœ… Easy to remove/disable

πŸ› Troubleshooting

Not working?

  • Clear all caches (browser, WordPress, CDN)
  • Check if BuddyPress Profile Pro is active
  • Verify the field type is supported

Wrong order?

  • Check for extra spaces in option values
  • Ensure special values match exactly (case doesn't matter)

πŸ“Œ Notes

  • Original admin order is preserved (sorting only affects frontend)
  • Works with profile edit and registration forms
  • Compatible with AJAX-loaded content
  • No performance impact on large lists

πŸ”„ Compatibility

  • BuddyPress Profile Pro: 2.5.0+
  • WordPress: 5.0+
  • PHP: 7.0+
  • Works with BuddyBoss Platform

Save hours of manual list management with automatic alphabetical sorting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment