Reply To: How to access search phrase via add_filter

Home Forums Product Support Forums Ajax Search Pro for WordPress Support How to access search phrase via add_filter Reply To: How to access search phrase via add_filter

Ernest Marcinko
Ernest Marcinko


Sorry for the late response, I messed up my forum notifications and missed a few tickets.

Well, I’ve looked into this in more detail and the problem is that there is no action/filter executed before printing the results list. Even bigger problem is that the data printed is delimited between two strings to avoid any error messages to appear in the results list in case something else is malfunctioning.

The only solution is to make a tiny modification in the search code and add then use a filter.

1. Open up the wp-content\plugins\ajax-search-pro\includes\classes\ajax\class-asp-search.php file and scroll to lines 87-89:

if (isset($_POST['asp_get_as_array'])) {
    return $results;

..just below these 3 lines add this extra line:

$html_results = apply_filters('asp_before_ajax_output', $html_results);

This will enable access to the final HTML results list.

2. Now, use this filter to print extra information to the ajax response:

add_filter('asp_before_ajax_output', 'asp_print_before_first_result', 1);
function asp_print_before_first_result( $html ) {
    $search_id = $_POST['asid'];
    $phrase = $_POST['aspp'];
    $ids = array(1, 2, 3); // The search IDs you want to use this code on
    if ( in_array($search_id, $ids)) {
      $html = "<div class='asp_the_phrase'>Results for: ".esc_sql($phrase)."<div>" . $html;
    return $html;

Don’t forget to change the $ids array to the ids you want the code to apply on.

This appears to be working on my test environment, but let me know.

Ernest Marcinko

If you like my products, don't forget to rate them on codecanyon :)