Skip to content

Instantly share code, notes, and snippets.

@jchristopher
jchristopher / gist:10211284
Last active August 29, 2015 13:58
Use SearchWP in conjunction with Dave's WordPress Live Search. To utilize, add the following to your theme's functions.php and save yourself the trouble of editing the plugin files.
<?php
function my_searchwp_dwls_alter_results( $search_results, $deprecated, $daves_live_search ) {
global $wp_query;
if( class_exists( 'SearchWP' ) ) {
// remove Dave's pre_get_posts
remove_action( 'pre_get_posts', array( 'DavesWordPressLiveSearchResults', 'pre_get_posts' ) );
<?php get_header(); ?>
<?php
$number_of_feature_posts = 1;
$number_of_secondary_posts = 8;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$how_many_secondary_posts_past = ($number_of_secondary_posts * ($paged - 1));
$off = $number_of_feature_posts + (($paged > 1) ? $how_many_secondary_posts_past : 0);
?>
<?php
// add support for strings like M&M in SearchWP
function my_searchwp_term_pattern_whitelist( $patterns ) {
$patterns[] = "/\\b([[:alnum:]]+\\s?(?:&\\s?[[:alnum:]]+)+)/is";
}
add_filter( 'searchwp_term_pattern_whitelist', 'my_searchwp_term_pattern_whitelist' );
@jchristopher
jchristopher / gist:10730056
Created April 15, 2014 12:53
Throttle the SearchWP indexer
<?php
// pause the indexer for 1s in between passes
function my_searchwp_indexer_throttle() {
return 1;
}
add_filter( 'searchwp_indexer_throttle', 'my_searchwp_indexer_throttle' );
<?php
/* Template Name: Advance Search Template */
global $post;
// retrieve our search query if applicable
$query = isset( $_REQUEST['swpquery'] ) ? sanitize_text_field( $_REQUEST['swpquery'] ) : '';
// retrieve our pagination if applicable

Keybase proof

I hereby claim:

  • I am jchristopher on github.
  • I am jchristopher (https://keybase.io/jchristopher) on keybase.
  • I have a public key whose fingerprint is C446 E843 8699 4276 7D70 3CF4 5C0A BFF4 8230 0414

To claim this, I am signing this object:

<?php
function my_admin_notices() {
$success = true;
if( $success ) {
?>
<div class="updated">
<p><?php _e( '<strong>SUCCESS</strong> Success!', 'mytextdomain' ); ?></p>
</div>
<?php
@jchristopher
jchristopher / gist:2c635560de32191da7a5
Created April 30, 2014 15:47
If you're using get_search_form() to generate a search form on bbPress Forum, Topic, or Reply pages and want to limit search results to the current Forum, use this snippet
<?php
function my_get_search_form( $html ) {
// we need to append the current Forum ID so our future hook can use it
if( function_exists( 'bbp_get_forum_id' ) ) {
$forum_id = isset( $_REQUEST['swpforumid'] ) ? absint( $_REQUEST['swpforumid'] ) : bbp_get_forum_id();
if( ! empty( $forum_id ) ) {
$html = str_replace( '</form>', '<input type="hidden" name="swpforumid" value="' . $forum_id . '" /></form>', $html );
}
}
@jchristopher
jchristopher / gist:226c751aab6993a04385
Last active August 29, 2015 14:01
Enable SearchWP Live Search on any search form input manually
<?php
// if your form is generated using get_search_form() you do not need to do this
// as SearchWP Live Search does it automatically out of the box
?>
<form action="" method="get">
<p>
<label for="s"><?php _e( 'Search' , 'mytextdomain' ); ?></label>
<input type="text" name="s" id="s" value="" data-swplive="true" /> <!-- data-swplive="true" enables SearchWP Live Search -->
</p>
@jchristopher
jchristopher / gist:b7208de4d1e62220ad6e
Created May 5, 2014 14:24
Prevent SearchWP Live Search from automatically enabling live search on forms generated with get_search_form()
<?php
add_filter( 'searchwp_live_search_hijack_get_search_form', '__return_false' );