Last active
February 1, 2024 10:19
-
-
Save nicmare/7ea3978dc74fd5c4c04fa17dc99c8407 to your computer and use it in GitHub Desktop.
This Code Snippet modifies the Wordpress Search Terms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function search_replacement($term){ | |
return ltrim(str_replace( array('0x'), array('0 x '), urldecode(strtolower($term)) )); | |
} | |
// ajax search: | |
function replace_search( $query_object ) | |
{ | |
if( $query_object->is_search() ) { | |
$raw_search = $query_object->query['s']; | |
$query_object->set( 's', search_replacement($raw_search) ); | |
} | |
} | |
add_action( 'parse_query', 'replace_search' ); | |
// regular search: | |
function replace_regular_search( $q ) { | |
if ( $q->is_main_query() && $q->is_search() && ! is_admin() && !empty($q->query["s"])) { | |
$q->set("s",search_replacement($q->query["s"])); | |
} | |
} | |
add_action('pre_get_posts', 'replace_regular_search'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment