Skip to content

Instantly share code, notes, and snippets.

<?php
@link https://searchwp.com/v3/docs/kb/adding-metrics-link-tracking-to-listify-via-facetwp/
// Add these lines BEFORE the existing template code.
do_action( 'searchwp_metrics_click_tracking_start' );
add_action( 'listify_output_results', function() {
do_action( 'searchwp_metrics_click_tracking_stop' );
} );
@jchristopher
jchristopher / searchwp-customizations.php
Last active June 15, 2020 15:56
Add child PDF snippets to SearchWP result excerpt
<?php
// Add child PDF snippets to SearchWP result excerpt.
add_filter( 'get_the_excerpt', function( $excerpt ) {
global $post;
if ( ! $post instanceof WP_Post || ! is_search() || post_password_required() || $post->searchwp_excerpt_found ) {
return $excerpt;
}
@jchristopher
jchristopher / searchwp-customizations.php
Last active February 1, 2021 20:30
Use a Custom Field as a buoy to supercede SearchWP's relevance weight sorting
<?php
// Use a Custom Field as a buoy to supercede SearchWP's relevance weight sorting.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
global $wpdb;
$meta_key = 'my_buoy_meta_key';
// Add the buoy to these post types:
$post_types = [ 'post', 'page', ];
@jchristopher
jchristopher / searchwp-customizations.php
Created April 11, 2020 00:18
Limit SearchWP results to the current bbPress Forum when applicable
<?php
// Customize native WordPress search form to include current bbPress Forum ID if applicable.
add_filter( 'get_search_form', function( $markup ) {
if ( ! function_exists( 'bbp_get_forum_id' ) ) {
return $markup;
}
$forum_id = isset( $_REQUEST['swpforumid'] ) ? absint( $_REQUEST['swpforumid'] ) : bbp_get_forum_id();
@jchristopher
jchristopher / searchwp-customizations.php
Last active April 10, 2020 15:33
Add a Shortcode to exclude content from SearchWP's index
<?php
// Add a Shortcode to exclude content from SearchWP's index.
add_shortcode( 'searchwp_no_index', function( $atts, $content = null ) {
// If the indexer is running do not return anything,
// else return the content contained in the Shortcode.
return did_action( 'searchwp\indexer\batch' ) ? '' : $content;
} );
@jchristopher
jchristopher / page-template.php
Last active November 11, 2020 13:48
Basic usage of SearchWP's Highlighter class
<?php
$highlighter = new \SearchWP\Highlighter();
$haystack = 'The best coffee you can find!';
$needle = 'coffee';
// Output:
// The best <mark class="searchwp-highlight">coffee</mark> you can find!
echo $highlighter->apply( $haystack, $needle );
@jchristopher
jchristopher / functions.php
Last active April 4, 2023 12:27
Use Elementor to power SearchWP Supplemental Engine
<?php
// @link https://searchwp.com/v3/docs/kb/using-elementor-for-supplemental-engines/
// We need to flag the search form.
add_action( 'elementor_pro/search_form/after_input', function( $form ) {
// Check to see if this is the right Search Form.
$settings = $form->get_data( 'settings' );
<?php
// @deprecated
// @see https://searchwp.com/documentation/hooks/searchwp-background_process-http_basic_auth_credentials/
class MySearchWPBasicAuthCreds {
private $username = 'username'; // HTTP Basic Auth username.
private $password = 'password'; // HTTP Basic Auth password.
function __construct() {
// Provide HTTP Basic Authentication credentials to SearchWP.
@jchristopher
jchristopher / searchwp-customizations.php
Created April 1, 2020 12:44
Index WooCommerce Product Variation SKUs in SearchWP.
<?php
// @link https://searchwp.com/documentation/knowledge-base/woocommerce-product-variation-skus/
// Index WooCommerce Product Variation SKUs alongside the parent Product.
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
$entry = $entry->native();
if ( ! $entry instanceof \WP_Post || 'product' !== get_post_type( $entry ) ) {
return $data;
}
@jchristopher
jchristopher / searchwp-customizations.php
Last active April 14, 2021 15:36
Add bonus weight from Custom Field value in SearchWP
<?php
// Add bonus weight from Custom Field value in SearchWP.
add_filter( 'searchwp\query\mods', function( $mods ) {
global $wpdb;
// Custom Field name. Needs to store data as YYYYMMDD (ACF does this already).
$my_meta_key = 'date_field';
$mod = new \SearchWP\Mod();