Can you add the hook in these 3 functions?

We want to create a custom filter element. We checked the bricks filter files. We need the hook in these 3 methods.

Can the Bricks Team add the hooks this way?

If we get the hooks there, then we can easily create the custom filter element as per client request.

1 Like

Hi,
This will be a great addition for extenders of Bricks.

In meantime,
you could potentially implement new Filter Elements - right now - using bricks/builder/elements available hook, defined here:

// in themes/bricks/includes/elements.php
// Lines: 105


// Load filter element base class (@since 1.9.6)
if ( Helpers::enabled_query_filters() ) {
	require_once BRICKS_PATH . 'includes/elements/filter-base.php';
	$input_elements = [
		'filter-checkbox',
		'filter-datepicker',
		'filter-radio',
		'filter-range',
		'filter-search',
		'filter-select',
		'filter-submit',
	];

	$element_names = array_merge( $element_names, $input_elements );
}

$element_names = apply_filters( 'bricks/builder/elements', $element_names );

You can do something like

// in your plugin 

add_filter( 'bricks/builder/elements', function ($elements) {
  // add only if Bricks > Settings > Filters are enabled...
  if (Bricks\Helpers::enabled_query_filters()) {
    $elements = array_merge(  $elements,  ["my-new-filter"]  );
  }
  return $elements;
});

Code not tested.

1 Like

Thank you for this information. It will help for other purposes. What I am trying to achieve now, I think that this method will not work.

But I shall try your approach and test it.

1 Like

Unfortunately, it is not working. I am trying to achieve this and customize things for swatches.

This is my first test.

Screenshot 2024-07-12 at 9.30.20 PM

TLDR: @bricksultimate is right. It’s not possible to register custom Bricks Filter elements and inherit the behavior of the Filter_Base class until it’s possibile to extend the “allowed filter element” white list.

Long Answer

You are right.
Tested the code i provided - in a previous comment - but it’s wrong.

The correct code for inserting a new bricks element is this:

/**
 * Register custom elements
 */
add_action('init', function () {
  // register custom elemets
  $element_files = [];

  // if Bricks > Settings > Filters are enabled add also elements prefixed with "filter-"
  if (Bricks\Helpers::enabled_query_filters()) {
    $element_files = array_merge(  $element_files,  [
      __DIR__ . '/filter-color-swatches.php'
    ]);
  }

  // register each element
  foreach ($element_files as $file) {
    \Bricks\Elements::register_element($file);
  }

}, 11);

The element is succesfully registered…
But is not enough to make it work.
When we add the elemnt in the Builder, the Builder perform a request to the server, in order to render the element settings panel and the HTML of the preview.
In this moment Bricks checks if the element is whitelisted in a list of “allowed elements of type filter”.

// bricks/includes/elements/filter-base.php
	public function get_filter_controls() {
		if ( ! in_array( $this->name, Query_Filters::supported_element_names() ) ) {
			return [];
		}

Because the element is not whitelisted, to result is that the builder show no settings in the “Content” tab.

Conclusion

The initial request of this topic is valid.
Adding these 3 filters will be a first move into a Custom Filter API.

Additional note

Going deeper into that, also other filters need to be created in order to allow the exetender to create a new Filter elements and inherit default behavior of a Bricks native filter element.

1 Like

Because @bricksultimate want to add Color Swatches, it could be beneficial to add a “filter hook” into the “render” function of native Bricks Filter’s elements.

That way, the logic of filter is handled by the native element, and only the visual part (HTML) can be customized.

Yes. You got the point and 100% correct. If you do not whitelist the custom filter element, the index is not created in the table and the filter does not work.

@moderators Can you please delete this post?