Replace result-urls with something else

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Replace result-urls with something else

This topic contains 3 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 3 years, 8 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #28776
    Julius89
    Julius89
    Participant

    Hello!
    I would like to replace only particular result-urls (not all urls) with other urls on the live-results.
    So for example

    websiteurl1.com should be replaced with google.com
    and
    websiteurl2.com with wikipedia.com
    and
    websiteurl3.com should not be changed.

    I found this documentation on the ajax search pro-website: https://knowledgebase.ajaxsearchpro.com/miscellaneous/other/how-to-change-the-results-url-to-something-else

    Could you point out which code-snippets I have to modify to make this happen?

    Thank you very much

    Julius

    #28777
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Julius,

    You are looking for this code snippet:

    add_filter( 'asp_results', 'asp_custom_link_results', 10, 1 );
    function asp_custom_link_results( $results ) {
    
      // Parse through each result item
      foreach ($results as $k=>$v) {
        /**
         * In this context the
         *      $results[$k]->link
         * variable holds the result link. Make modifications to that variable.
         */
        $results[$k]->link  = 'https://google.com';
      }
    
      return $results;
    }

    I would recommend something like this:

    add_filter( 'asp_results', 'asp_custom_link_results', 10, 1 );
    function asp_custom_link_results( $results ) {
    	// Array of original -> replacement links
    	$replace = array(
    		'websiteurl1.com' => 'google.com',
    		'websiteurl2.com' => 'wikipedia.com'
    	);
    
    	// Parse through each result item
    	foreach ($results as $k=>&$r) {
    		/**
    		 * In this context the
    		 *      $results[$k]->link
    		 * variable holds the result link. Make modifications to that variable.
    		 */
    		$r->link  = str_replace(array_keys($replace), array_values($replace), $r->link);
    	}
    
      return $results;
    }

    Just enter the values to the $replace variable. On each line, the left side is the value to replace, with the value on the right side.

    Best,
    Ernest Marcinko

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


    #28809
    Julius89
    Julius89
    Participant

    Hi!

    The result-linke isn’t replaced properly but instead “google.com” is added to the existing link.

    But this is no problem for me. I have chosen a different solution with ajax search pro for my project, that doesn’t rely on this customization. So I don’t want to replace the result-link anymore.

    Thanks for your efforts! I like Ajax Search Pro very much.

    Julius

    #28812
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

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


Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.