Frontend filters API
API for adding/modifying and removing front-end plugin filter boxes
Changing filter values within a filter box
To access and change the values of filter items use the asp_pre_get_front_filters hook, as following:
add_filter('asp_pre_get_front_filters', 'asp_change_a_filter', 10, 2);
function asp_change_a_filter($filters, $type) {
foreach ($filters as $k => &$filter) {
// To check some of the attributes:
// $filter->label
// $filter->display_mode
// $filter->position
// $filter->id
// $filter->type() >> returns the filter type
// $filter->field() >> field that is filtered (if applicable)
// do a var_dump($filter->data) -> to see the filter data
// Go through the filter items via a loop
foreach ($filter->get() as $kk => $item) {
// Changing a filter attribute: label, selected, value, default by array ID
$filter->attr($kk, 'label', 'New Label', true);
// Remove the current item by array ID
$filter->remove($kk, true);
}
// You can also change/remove items by key, without a loop
// ..for a category/term filter, use the term ID
$filter->attr(123, 'label', 'New Label');
$filter->remove(123);
// ..for a custom field filter, use the field value
$filter->attr('value', 'label', 'New Label');
$filter->remove('value');
// Remove values from a custom field filter
if ( $filter->type() == 'custom_field' && $filter->field() == 'my_field' ) {
$filter->remove(array('value1', 'value2'));
}
// Adding a new value to a custom field filter
if ( $filter->type() == 'custom_field' && $filter->field() == 'my_field' ) {
$filter->add(array(
'label' => 'Value label 1',
'selected' => false,
'value' => 'value1'
));
// The second function argument allows setting the value position
// Adding a value after the 2nd option
$filter->add(array(
'label' => 'Value label 2',
'selected' => false,
'value' => 'value2'
), 2);
// Adding a value to before the last option
$filter->add(array(
'label' => 'Value label 3',
'selected' => false,
'value' => 'value3'
), -1);
}
}
return $filters;
}The WD_ASP_FrontFilters singleton class
This API should be used to manage the front-end filter boxes. The class methods can be used to add/remove/modify/find any front-end filter box.
Source
File: wp-content/plugins/ajax-search-pro/includes/classes/core/class-asp-frontfilters.php
The front-end filter management is encapsulated within the WD_ASP_FrontFilters class, and it's instance can be accessed via the..
..variable, or if you prefer your own, then:
Usage
For usage examples, please see the chapter below.
Filters should be added, changed and removed exlusively within the asp_pre_parse_filters and asp_post_parse_filters action hooks. Using any other hooks may result in a malfunction.
Example of changing and removing certain filters by their labels:
Example of adding a custom taxonomy filter:
For methods list and examples, please check below.
Methods list
create()
Creates and returns a new filter object, depending on the $type variable. This will not add the filter automatically to the filters list, only creates a new object. The filter has to be added via the add($filter) method after.
Parameters
$type (string) - The filter type, can be: taxonomy
$label (string) (optional) - The filter box header label
$display_mode (string) (optional) - The display mode of the filter values: checkboxes, input, slider, range, dropdown, radio, dropdownsearch, multisearch
$data (array) (optional) - Additional data, that may be required within the template for this filter depending on the $type and $display_mode Check the examples below for the usage.
Return values
(aspFilter | aspCfFilter | aspTaxFilter) The new filter object
add()
Adds the final $filter object to the front-end filters list.
Parameters
(aspFilter) $filter - The filter object to add to the front-end filters list
Return values
(aspFilter | aspCfFilter | aspTaxFilter) The new filter object
set()
Finds a filter by title or ID ($key) and changes it's attribute.
Parameteres
$key (int|string) - Filter ID or filter label text
$attribute (string) - Filter attribute: label, display_mode, data, position
$value (mixed) - Value to change the attribute to
Return values
(bool) true|false - True, when the change was successful, false otherwise.
get()
Gets the registered filters by the given order and the given type
Parameteres
$order (string)(optional) - position or added
$type (bool | string)(optional) - filter type: taxonomy, custom_field or boolean false for everything
Return values
array - Array of front-end filters
remove()
Removes a filter from the front-end filters list, based on the ID or filter label.
Parameters
$key (int|string) - Filter ID or filter label text
Return values
(bool) true|false - True, when the removal was successful, false otherwise.
Examples
Adding custom taxonomy filter
The example below displays adding a custom taxonomy filter, with a select all option. The commented sections contain the possible parameters for this type of filter.
Dropdown, checkbox and radio type custom field filters
Text and hidden type custom field filters
Date type custom field filter
Slider and Range slider custom field filter
Last updated