• Resolved webmestrecsn

    (@webmestrecsn)


    Is it possible to change the labels for the faceted search ?

    Our custom taxonomies are machine names that begin with a namespace (ex. nmspc_region and nmspc_colour). The namespace is machine readable, but it doesn’t make sense to the user.

    Is it possible to specify what label we want to appear in the list of facets in the search result (so that it reads “region” instead of “nmspc_region”) ?

    https://ww.wp.xz.cn/plugins/wpsolr-search-engine/

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Change label for facets ?’ is closed to new replies.