-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathac-filtering-select-options.php
More file actions
81 lines (70 loc) · 2.32 KB
/
ac-filtering-select-options.php
File metadata and controls
81 lines (70 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* The `ac/filtering/select/options` filter modifies the options shown in the filter bar
* dropdown for a column. This applies to the persistent filter dropdowns above the list table,
* as opposed to `ac/search/select/options` which applies to Smart Filtering.
*
* Each option in the array has the shape: ['id' => 'value', 'text' => 'Label']
*/
/**
* @param array $options The current list of options. Each item: ['id' => string, 'text' => string]
* @param AC\Column\Context $column The column whose filter dropdown is being populated.
* @param AC\ListScreen $list_screen The list screen the filter belongs to.
*
* @return array
*/
add_filter(
'ac/filtering/select/options',
static function (array $options, AC\Column\Context $column, AC\ListScreen $list_screen): array {
return $options;
},
10,
3
);
/**
* Example: rename or reformat option labels for a specific custom field filter dropdown.
* Useful when the stored values are not human-readable (e.g. numeric IDs or slugs).
*/
add_filter(
'ac/filtering/select/options',
static function (array $options, AC\Column\Context $column): array {
if ( ! $column instanceof AC\Column\CustomFieldContext) {
return $options;
}
if ($column->get_meta_key() !== 'my_status_field') {
return $options;
}
$labels = [
'draft' => 'In Progress',
'review' => 'Awaiting Review',
'approved' => 'Approved',
];
foreach ($options as &$option) {
if (isset($labels[$option['id']])) {
$option['text'] = $labels[$option['id']];
}
}
return $options;
},
10,
2
);
/**
* Example: prepend a custom "All" option to the filter dropdown so users can
* explicitly reset the filter without having to clear it another way.
*/
add_filter(
'ac/filtering/select/options',
static function (array $options, AC\Column\Context $column): array {
if ( ! $column instanceof AC\Column\CustomFieldContext) {
return $options;
}
if ($column->get_meta_key() !== 'my_custom_field') {
return $options;
}
array_unshift($options, ['id' => '', 'text' => '— All —']);
return $options;
},
10,
2
);