wpfts_widget_html (Filter)
The wpfts_widget_html
filter in WP Fast Total Search allows developers to modify the HTML code of the search widget before it’s output on the page. This provides complete control over the widget’s markup, enabling the addition, modification, or removal of HTML elements.
When to Use
This filter is useful in the following scenarios:
- Adding new elements to the widget: For example, you can add a field to select a search category or other controls.
- Modifying existing widget elements: You can change element attributes, such as adding CSS classes or styles.
- Removing widget elements: You can hide or remove unnecessary elements.
- Completely replacing the widget markup: You can completely override the widget’s HTML code.
Arguments
$out
(string): The HTML code of the widget, generated by the plugin.$preset
(array): The array of preset data used by the widget.$preset_id
(string): The ID of the preset.$context
(string): The context of the widget usage (in this case, ‘widget’).
Return Value
$out
(string): The modified HTML code of the widget.
Example (Adding a Category Selection Field)
add_filter('wpfts_widget_html', 'add_category_select', 10, 4);
function add_category_select($out, $preset, $preset_id, $context)
{
$categories = get_categories();
$select = '<select name="category">';
foreach ($categories as $category) {
$select .= '<option value="' . $category->term_id . '">' . $category->name . '</option>';
}
$select .= '</select>';
// Insert the select before the search field.
$out = str_replace('<input type="search"', $select . '<input type="search"', $out);
return $out;
}
Important Notes
- The
wpfts_widget_html
filter is called before the widget’s HTML code is output on the page. - Changes made to
$out
will be reflected on the website. Exercise caution when modifying the HTML code. - The
$preset
,$preset_id
, and$context
parameters can be used to dynamically change the HTML code depending on the widget settings.
The wpfts_widget_html
filter is a powerful tool for customizing the WPFTS search widget, giving developers complete control over its markup.