Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Multiple questions ( CPT , size title ; stats ) › Reply To: Multiple questions ( CPT , size title ; stats )
March 21, 2022 at 2:54 pm
#37231
Keymaster
Thanks.
Okay, the context feature should execute on the {descriptionfield} section, but no on {description_fr}{description_en} section. I’m afraid there is no easy way of forcing that behaviour.
You can try a custom code, but that will be much less efficient, and may not work in all cases.
Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.
function text_context_find($s, $content) {
if ( $s !== '' ) {
$regex = '/([A-Za-z0-9.,-]+\s*){0,5}\s'.$s.'(\s|[,.!?])(\s*[A-Za-z0-9.,-]+){0,40}/';
preg_match($regex, $content, $matches);
if ( $matches[0] !== '' ) {
if ( str_starts_with($content, $matches[0]) ) {
if ( $matches[0] != $content ) {
return $matches[0] . '...';
} else {
return $matches[0];
}
} else {
return '...' . $matches[0] . '...';
}
}
}
return $content;
}
function asp_context_find( $results, $search_id, $is_ajax, $args ) {
foreach ($results as $k=>&$r) {
$r->content = text_context_find($args['s'], $r->content);
}
return $results;
}
add_filter( 'asp_results', 'asp_context_find', 10, 4 );