Plugin Author
WPSOLR
(@wpsolr)
There is a filter for that:
add_filter( WpSolrFilters::WPSOLR_FILTER_SEARCH_PAGE_FACET_NAME, array(
$this,
'my_filter_hook'
), 10, 1 );
Hello,
In what file can I add this filter? Is it possible to add it to a template or do I have to hack the wpsolr plugin?
Thanks!
-Emmanuel
Plugin Author
WPSOLR
(@wpsolr)
Anywhere you want. Template, functions.php …
If I put the filter in functions.php in my theme, by what should I replace ‘$this’ in the arg array?
Plugin Author
WPSOLR
(@wpsolr)
This is the syntax for a static method.
Use the standard add_filter with a function hook if you prefer.
Here is an example of the call to the filter:
// Give plugins a chance to change the facet name (ACF).
$facet_to_display_name = apply_filters( WpSolrFilters::WPSOLR_FILTER_SEARCH_PAGE_FACET_NAME, $facet_to_display_id_without_str );
Thanks. We used this as follows (object oriented context).
First, a function to check if wpsolr plugin is active:
/**
* Checks if the required plugin is active in network or single site.
*
* @source http://queryloop.com/how-to-detect-if-a-wordpress-plugin-is-active/
*
* @param $plugin
*
* @return bool
*/
function plugin_is_active( $plugin ) {
$network_active = false;
if ( is_multisite() ) {
$plugins = get_site_option( 'active_sitewide_plugins' );
if ( isset( $plugins[$plugin] ) ) {
$network_active = true;
}
}
return in_array( $plugin, get_option( 'active_plugins' ) ) || $network_active;
}
Then in the constructor of the object used as a ‘facet’:
//if the 'Apache Solr search by WPSOLR plugin' is active, hook into a filter that
// allows control over the text displayed for WPSolR 'facets'
if( $this->plugin_is_active( 'wpsolr-search-engine/wpsolr_search_engine.php' ) ) {
//wait until the plugins are loaded to calculate the text to make sure the filter
// is available
add_action( 'plugins_loaded', array( $this, 'update_wpsolr_facet_labels' ) );
}
And the functions:
/**
* A filter function for WPSolR, a WordPress SolR plugin, that ensures the
* taxonomy's label is shown when displaying a 'facet', rather than the $machine_name.
*
* @param String $machine_name The original name used for the facet.
* Expected as the name saved in the database to
* refer to the taxonomy.
* @return String $label The text to display for the facet on the search page.
*/
public function get_label_for_wpsolr( $machine_name ) {
//set the original facet name as the default value
$label = $machine_name;
//if the facet is this taxonomy object, return the actual label of the taxonomy
if ( $this->machine_name === $machine_name ) {
$label = $this->label;
}
//pass the result to WPSolR to display on the search page
return $label;
}
/**
* Filter the text used to display the names of taxonomies when they are
* used as 'facets' by the Apache Solr search by WPSOLR plugin.
*
* @return void
*/
public function update_wpsolr_facet_labels() {
//hook into WPSolR, an additional plugin
add_filter( WpSolrFilters::WPSOLR_FILTER_SEARCH_PAGE_FACET_NAME, array( $this, 'get_label_for_wpsolr' ), 10, 1 );
}
Plugin Author
WPSOLR
(@wpsolr)
Thanks for the code example.