Add forum search results to WordPress search

Hello all. I finally found a little slice of time to try pulling search results from Discourse into Wordpress’s search. The gist is that I created a separate script which uses the Discourse API to do a search query and then I just include this as an iframe in the Wordpress search template.

This may not be the ideal solution for everyone because it show the Discourse results in a separate column from the Wordpress results, not all mixed together.

Here’s a screenshot of how it looks. The forum results are in the grey column next to the Wordpress results:

Step 1:

Create a standalone php script called something like discoursesearch.php and upload it wherever you want to your server:

<?php

define('WP_USE_THEMES', false);
require_once('./wp-blog-header.php');

if (isset($_GET['s'])) {
    $search_term = str_replace('+',' ',$_GET['s']);
    $search_term = str_replace('\"','"',$_GET['s']);
}else{
    exit;
}

// change this to your forum's info
$api_key="YOURAPIKEY";
$api_user="system";
$url_base="https://www.yourforum.com/";
$api_auth="api_key=" . $api_key . "&api_username=" . $api_user;

$ch = curl_init();
$url = $url_base . "search/query.json?term=".urlencode($search_term)."&" . $api_auth;

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$response = curl_exec($ch);	

$response_data = json_decode($response, true);

if ( !isset($response_data['errors']) )	{
	
	$all_posts = $response_data['posts'];
	$all_topics = $response_data['topics'];
	
	$num_results = count($all_posts);
	
	if ( $num_results > 0 )
		echo '<h3>Forum Discussions</h3>';

	for ( $i=0; $i<$num_results; $i++ ) {
		
		// see here for all the fields that can be parsed from the search results http://docs.discourse.org/#tag/Search
		$topic_title = $all_topics[$i]['title'];
		$result_url = $url_base . 't/' . $all_topics[$i]['slug'] . '/' . $all_posts[$i]['topic_id'] . '/' . $all_posts[$i]['post_number'];
		$blurb = str_ireplace($search_term,'<b>'.$search_term.'</b>',$all_posts[$i]['blurb']);
		$username = $all_posts[$i]['username'];
		$avatar = $url_base . str_replace('{size}','30',$all_posts[$i]['avatar_template']);
		echo '<p><a target="_new" href="'.$url_base.'u/'.$username.'/summary"><img src="'.$avatar.'" align="left"></a><a target="_new" href="'.$result_url.'">' . $topic_title . '</a>' . '</p><p>' . $blurb . '</p>';
		
	}
	
	echo '<p><a target="_new" href="'.$url_base.'search?q='.$search_term.'">See all forum discussions about: '.$search_term.'</a></p>';
}
?>

Step 2:

Then open up the search template file for your Wordpress theme and put this where ever you’d like the search results to appear:

<iframe src="./discoursesearch.php?s=<?php echo urlencode(get_search_query());?>" height="1000" width="100%" style="height:1000px;width:100%;"></iframe>

That’s it. A bit quick and dirty but it does the job. Hopefully this will be helpful to someone! :slight_smile:

19 Likes

Great!

30

(probably want to remove that hardcoded URL thingy there :wink:

1 Like

Note: this doesn’t show the threads for me due to the X-Frame options

Thanks @Bas! I’m embarrassed by my sloppiness. haha! :smiley:

@Bas, perhaps that search api code will be handy anyway. It could be put right into the search template rather than via an iframe. I kinda like the iframe because it doesn’t slow down the search page at all.

1 Like

I think this is a XSS vector - you need to urlencode(get_search_query())

5 Likes

Thanks. Probably a safe idea. I’ve updated the code.

2 Likes

Thanks very much I need that feature!
Just to be sure, is it the only things I need to change?

$api_key=“YOURAPIKEY”;
$url_base=“https://www.yourforum.com/”;

It’s been a while since I looked at this code but I’m pretty sure that’s all you have to change.

Thanks for the reply.

I changed my api key and url but it is only what I see :

Search for the word “cours” in my forum : Résultats de recherche pour « cours » - Le peuplier

Search for the word “cours” in my test wordpress site : https://test.lepeuplier.ca/?s=cours

53

If I click on “see all discussions”, the URL is : https://forum.lepeuplier.ca/search?q=%3C?php%20echo%20urlencode(get_search_query());?%3E

Hi @Francois_Douville. Where exactly did you save your “discourse.php” file?

Can you try calling it directly like:

https://www.yourwordpresssite.com/discoursesearch.php?s=cours

In my folder here in my Cpanel. Don’t know if it’s the right place…

I don’t understand. Where should I put this url?

OK I was just saying you can test the discourse.php search directly by putting this in your browser url area:
https://test.lepeuplier.ca/discoursesearch.php?s=cours

I can see by doing the above that it doesn’t work. Can you access your php error_log and see what it says?

One possible issue I can think of is this – the code relies on the function curl and some hosts have it disabled.

Any errors in the error_log related to discoursesearch.php would be helpful to see.

1 Like

I don’t seem to have any error log

I asked to support at Inmotion Hosting and it is at the right place. Don’t understand why it’s not working…

Do you have any idea why it doesn’t work @simon?

Hi @Francois_Douville, it may be worth asking inmotion to help. In particular, I’d ask them:

  1. to check on why your php error log is empty and whether it’s disabled for some reason

  2. if curl is enabled for php

Yes I already asked Inmotion and curl is enabled.

My wordpress site is with Inmotion but my Discourse forum is with DigitalOcean. Is it with DigitalOcean that I have to check?

I’m having a similar problem, but when I go directly to my php it does display:
https://naturephotographers.network/discoursesearch.php?s=sellers

But search results do not:
https://naturephotographers.network/?s=sellers

My error logs do show, but I don’t see anything pertinent…

1 Like

Looks like one thing that’s going wrong for both if you is that discoursesearch.php not being included properly within your WordPress search template file (typically search.php).

The code is like so:

<iframe src="/discoursesearch.php?s=<?php echo urlencode(get_search_query());?>" height="1000" width="100%" style="height:1000px;width:100%;"></iframe>

but it looks like for both if you, the <?php...?> part is not being evaluated as php but is being passed into the script as a whole. Feel free to send me a Private Message here with the contents of your search.php file and I’ll see if I can figure out why this is happening. (So just copy and paste the code in search.php for me to see.)

3 Likes

Ok @lkramer told me that she does not have time for that.

I know my error. I didn’t put this code in my search.php file :

<iframe src="/discoursesearch.php?s=<?php echo urlencode(get_search_query());?>" height="1000" width="100%" style="height:1000px;width:100%;"></iframe>

I thought it was on my page builder (ElementorPro) using HTML :

So, I have 2 search.php files, one for my page builder and one for my theme. Which one do I use to put the code in?

Thanks anyone who can help me!