Note: You are viewing the documentation for Version 4View Version 3 Docs

searchwp\tokens\regex_patterns

Since: 4.0.0

Table of Contents

SearchWP searches for regular expression pattern matches to keep certain strings in tact and avoid tokenization which may reduce the usefulness during search. By default there are a number of common regular expression patterns to extract strings of SKUs, dates, initials, function names, version numbers, and more.

You can use this hook to add your own pattern matches.

Note: Patterns should be provided in order of strictness; more strict first.

The default patterns are as follows:

[
// Function names.
"/\b(\\w+?)?\\(|[\\s\\n]\\(/is",
// Date formats.
'/\b([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})\b/is', // YYYY-MM-DD
'/\b([0-9]{1,2}-[0-9]{1,2}-[0-9]{4})\b/is', // MM-DD-YYYY
'/\b([0-9]{4}\\/[0-9]{1,2}\\/[0-9]{1,2})\b/is', // YYYY/MM/DD
'/\b([0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4})\b/is', // MM/DD/YYYY
// IP addresses.
'/\b(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\b/is', // IPv4.
// Initials.
"/\\b((?:[A-Za-z]\\.\\s{0,1})+)/isu",
// Version numbers: 1.0 or 1.0.4 or 1.0.5b1.
'/\b([a-z0-9]+(?:\\.[a-z0-9]+)+)\b/is',
// Serial numbers.
'/(?=\S*[-_])([[:alnum:]\-_]+)/ius', // Hyphen/underscore separator.
// Strings of digits.
"/\\b(\\d{1,})\\b/is",
// e.g. M&M, M & M.
"/\\b([[:alnum:]]+\\s?(?:&\\s?[[:alnum:]]+)+)\b/isu",
]
view raw tmp.php hosted with ❤ by GitHub

Parameters

Type Parameter Default Since
String{} $patterns See gist above 4.0.0

Examples

All hooks should be added to your custom SearchWP Customizations Plugin.

Add custom regular expression pattern

<?php
// Add custom regular expression pattern to SearchWP.
add_filter( 'searchwp\tokens\regex_patterns',
function( $patterns ) {
$my_patterns = [
"/([0-9]{1,2}\\/[0-9]{1,2})/is", // Retain measurement details.
);
// We want our pattern to be considered top priority.
$patterns = array_merge( $my_patterns, $patterns );
return $patterns;
}
);

How to use this code

Fix keyword search on your site. No coding required!

Now you can utilize all of the content that's gone unrecognized by native WordPress keyword search instantly with SearchWP

Get SearchWP