ulgm-frontend-datatables-length-menu
Filters the options available for the table row length selection in the frontend DataTables.
add_filter( 'ulgm-frontend-datatables-length-menu', $callback, 10, 1 );
Description
This filter allows developers to customize the "Show entries" dropdown menu in Ultimate GDPR Manager's frontend DataTables. Modify the array of displayed row counts to control pagination options for your users. It's applied before DataTables initialization.
Usage
add_filter( 'ulgm-frontend-datatables-length-menu', 'your_function_name', 10, 1 );
Parameters
-
$datatable_length_menu(mixed) - This parameter holds an array representing the available options for the number of rows displayed per page in the DataTable.
Return Value
The filtered value.
Examples
<?php
/**
* Modify the DataTables length menu to include a custom option for "All entries".
*
* This filter hook allows developers to alter the options presented in the
* DataTables length menu, typically used to control the number of items
* displayed per page. Here, we add an option to display all entries.
*
* The original function creates a $datatable_length_menu array which is
* a two-dimensional array:
* [
* [10, 25, 50, 100], // The numerical values for the length
* ['10', '25', '50', '100'] // The display text for each length
* ]
*
* @param array $datatable_length_menu The current DataTables length menu configuration.
* @return array The modified DataTables length menu configuration.
*/
add_filter( 'ulgm-frontend-datatables-length-menu', function( $datatable_length_menu ) {
// Ensure we have the expected structure before modifying.
if ( ! is_array( $datatable_length_menu ) || count( $datatable_length_menu ) !== 2 ) {
return $datatable_length_menu; // Return original if structure is unexpected.
}
// Extract the existing numerical values and display texts.
$current_lengths = $datatable_length_menu[0];
$current_labels = $datatable_length_menu[1];
// Define the new option: -1 for "All" and "All" as its display text.
$all_entries_value = -1;
$all_entries_label = esc_html__( 'All', 'your-text-domain' ); // Use translation function.
// Check if "All" is already present to avoid duplication.
if ( ! in_array( $all_entries_value, $current_lengths ) ) {
// Append the "All" option to the end of the arrays.
$current_lengths[] = $all_entries_value;
$current_labels[] = $all_entries_label;
// Reconstruct the array to be returned by the filter.
$datatable_length_menu = array(
$current_lengths,
$current_labels,
);
}
return $datatable_length_menu;
}, 10, 1 ); // Priority 10, accepts 1 argument.
?>
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/class-utilities.php:1217
public static function attr_datatables_length_menu( $attribute = '' ) {
// Set the default value
$datatable_length_menu_numbers = array( 10, 25, 50, 100 );
$datatable_length_menu_numbers_name = array( 10, 25, 50, 100 );
// Remove extra spaces
$attribute = trim( $attribute );
// Get the custom length menu attribute
$custom_length_menu = ! empty( $attribute ) ? preg_replace( "/[nr]/", ',', $attribute ) : $attribute;
$custom_length_menu = ! empty( $custom_length_menu ) ? explode( ',', $custom_length_menu ) : array();
// Check if there are valid values
if ( ! empty( $custom_length_menu ) ) {
// Override the default values
$custom_datatable_length_menu_numbers = array();
$custom_datatable_length_menu_numbers_name = array();
// Iterate the custom attribute
foreach ( $custom_length_menu as $page_length ) {
// Get the label of the page length. For example,
// the label of the page length "-1" is "All".
$page_length_parts = explode( ':', $page_length );
// Check if there are two parts. If there are, then
// the user defined a custom label for this page length too
if ( count( $page_length_parts ) >= 2 ) {
// Remove extra spaces from the page length number
// and from the page number label
$page_length_number = trim( $page_length_parts[0] );
$page_length_text = trim( $page_length_parts[1] );
} else {
// Remove extra spaces from the page length number
// and from the page number label
$page_length_number = trim( $page_length_parts[0] );
// Then we have to define the text, but as the user didn't
// define a custom text, we will just use the number
$page_length_text = $page_length_number;
}
// Check if the page number is really a number
// Otherwise, don't add it
if ( is_numeric( $page_length_number ) ) {
// Add the custom page length to the list of custom page lengths
$custom_datatable_length_menu_numbers[] = $page_length_number;
$custom_datatable_length_menu_numbers_name[] = $page_length_text;
}
}
// Check if we have at least one length menu
// If so, override the original variables
if ( count( $custom_datatable_length_menu_numbers ) > 0 ) {
// Override the default values
$datatable_length_menu_numbers = $custom_datatable_length_menu_numbers;
$datatable_length_menu_numbers_name = $custom_datatable_length_menu_numbers_name;
}
}
// Create a variable with both page length numbers and text
$datatable_length_menu = array(
$datatable_length_menu_numbers,
$datatable_length_menu_numbers_name,
);
// Return it
return apply_filters(
'ulgm-frontend-datatables-length-menu',
$datatable_length_menu
);
}