1. Home
  2. Knowledge Base
  3. Document Library Pro
  4. Developer Documentation

Diacritics sorting support

Document Library Pro includes support for diacritics-neutral sorting, which ensures that accented characters (like á, é, ñ, ü) are sorted alongside their non-accented counterparts (a, e, n, u). This is particularly useful for multilingual document libraries.

Enabling diacritics sorting

To enable diacritics sorting in your document library:

  1. Navigate to Documents → Settings → Advanced in your WordPress admin
  2. Make sure "Lazy load" is disabled
  3. Look for the "Diacritics sorting" option (located just below the "Accent-insensitive search" checkbox)
  4. Check the box to enable this feature
  5. Save your settings

Once enabled, the title column will automatically support diacritics-neutral sorting.

How it works

When diacritics sorting is enabled, Document Library Pro modifies the tables column configuration to use the diacritics-neutralise sorting type instead of the default html type. This ensures that:

  • "André" comes before "Antonio"
  • "Müller" is sorted with other "M" names
  • "José" appears in the correct alphabetical position

When using standard tables, sorting may not work as expected if a column contains links. This is because the sorting algorithm treats the entire anchor tag (<a>...</a>) as a plain string, which can interfere with accurate sorting, especially when accented characters are involved.

To ensure proper sorting behavior, especially when links and special characters are present, we recommend using lazy-loaded tables instead with diacritics sorting disabled.

Adding diacritics support to other columns

By default, only the title column receives automatic diacritics sorting support. If you need this functionality for other columns (like categories, or tags), you can extend it using the document_library_pro_data_config filter hook.

Basic implementation

Add the following code to your theme's functions.php file:

/**
 * Add diacritics sorting support to additional columns
 */
function dlp_custom_add_diacritics_sorting_to_columns( $config, $args ) {
    // Only apply if diacritics sorting is enabled in settings
    $settings = Barn2\Plugin\Document_Library_Pro\Util\Options::get_settings();

    if ( ! isset( $settings['diacritics_sort'] ) || $settings['diacritics_sort'] !== '1' ) {
        return $config;
    }

    // Check if columnDefs exists
    if ( ! isset( $config['columnDefs'] ) ) {
        return $config;
    }

    // Define which columns should use diacritics sorting
    $diacritics_columns = [
        'col-doc_categories', // Document categories
        'col-doc_tags',       // Document tags
        'col-doc_author',     // Document author
        // Add more column classes as needed
    ];

    // Apply diacritics sorting to specified columns
    $config['columnDefs'] = array_map(
        function ( $column_def ) use ( $diacritics_columns ) {
            if ( isset( $column_def['className'] ) && in_array( $column_def['className'], $diacritics_columns ) ) {
                $column_def['type'] = 'diacritics-neutralise';
            }
            return $column_def;
        },
        $config['columnDefs']
    );

    return $config;
}
add_filter( 'document_library_pro_data_config', 'dlp_custom_add_diacritics_sorting_to_columns', 10, 2 );

Column class names

Here are the common column class names you can target:

Column Class Name
Title col-title
Categories col-doc_categories
Tags col-doc_tags
Author col-doc_author

Simple example: single column

If you only need to add diacritics support to one specific column:

/**
 * Add diacritics sorting to document categories only
 */
function dlp_custom_add_diacritics_to_categories( $config, $args ) {
    $settings = Barn2\Plugin\Document_Library_Pro\Util\Options::get_settings();

    // Check if diacritics sorting is enabled
    if ( isset( $settings['diacritics_sort'] ) && $settings['diacritics_sort'] === '1' && isset( $config['columnDefs'] ) ) {
        $config['columnDefs'] = array_map(
            function ( $column_def ) {
                if ( isset( $column_def['className'] ) && $column_def['className'] === 'col-doc_categories' ) {
                    $column_def['type'] = 'diacritics-neutralise';
                }
                return $column_def;
            },
            $config['columnDefs']
        );
    }

    return $config;
}
add_filter( 'document_library_pro_data_config', 'dlp_custom_add_diacritics_to_categories', 10, 2 );

Important notes

  1. Enable the Feature First: The diacritics sorting option must be enabled in the plugin settings for the code to work and is only compatible with non lazy loaded tables.
  2. Column Class Names: Make sure you're using the correct column class names. You can inspect your table's HTML to find the exact class names if needed or use debug functions to debug the columnDefs array.

Related Articles

If searching the knowledge base hasn't answered your question, please contact support.