2

I would like to know if it possible to customize the default search module of Wordpress. I've a website dealing with teams and teams' news.

Some of those teams has their names written like this

  • H.M.S.
  • L.A. Dream
  • N.Y. Invaders

I would like that the search modules could be able to manage those teams in the results even if i look for strings like HMS, or LA Dream, or NY, instead of filling the characters with dots. Does anybody has some suggestion, or know a WP trick that at the moment i'm not aware of?

Thanks in advance

1 Answer 1

0

Search Terms Dotifier:

Here's one idea using the request filter to append "dotted" words of two or three characters that don't contain any dots, to the current search terms.

So if your search string is for example:

The LA Dreams

it will become:

The LA Dreams L.A.

Here's a demo plugin to modify the default search:

<?php
/**
 * Plugin Name: Search Terms Dotifier
 * Author:      birgire
 * Plugin URI:  http://wordpress.stackexchange.com/a/157972/26350
 * Version:     0.0.1
 */

add_filter( 'request', function( $request ) {
    if( ! is_admin() && isset( $request['s'] ) )
    {
        // Dont' dotify these words (lower case):
        $exclude = array( 'the', 'in', 'it', 'he', 'she', 'was' );

        // Loop over search terms:
        $words = explode( ' ', $request['s'] );
        foreach( (array) $words as $word )
        {
             // Words with two or three chars that don't contain any dots:
             if( mb_strlen( $word ) > 1
                 && mb_strlen( $word ) < 4 
                 && false === strpos( $word, '.' ) 
                 && ! in_array( mb_strtolower( $word ), $exclude, true ) 
             )
             {
                 // Append the 'dotted' word:
                 $words[] = join( '.', str_split( $word ) ) . '.';
             }
         }
         $request['s'] = join( ' ', $words );
    }
    return $request;
});

You can hopefully extend this further to your needs.

1
  • it works fine, great solution
    – axel
    Commented Aug 15, 2014 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.