# Add forum search results to WordPress search

**URL:** https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782
**Category:** Administrators
**Tags:** how-to
**Created:** [2017 年 8 月 25 日午後 2:15 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782 "2017-08-25T14:15:31Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [2017 年 8 月 25 日午後 2:15 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/1 "2017-08-25T14:15:31Z")

</div>

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:

 ![org-2017-08-25-10-08-52](https://global.discourse-cdn.com/meta/original/3X/2/f/2f9e492b5944772702602156dfc6a8fefe1af76f.jpg)

**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! 🙂

---

<div class="post-metadata">

### Author: ![Bas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bas/32/294929_2.png) [@Bas](https://meta.discourse.org/u/Bas)
#### Post date: [2017 年 8 月 25 日午後 2:36 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/2 "2017-08-25T14:36:14Z")

</div>

Great!

 ![30](https://global.discourse-cdn.com/meta/original/3X/1/d/1d4f33ab96e8f5af5ec536d840afeba8ed484893.png)

(probably want to remove that hardcoded URL thingy there 😉

---

<div class="post-metadata">

### Author: ![Bas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bas/32/294929_2.png) [@Bas](https://meta.discourse.org/u/Bas)
#### Post date: [2017 年 8 月 25 日午後 2:40 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/3 "2017-08-25T14:40:02Z")

</div>

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

> [@Discourse not shown in iframe](https://meta.discourse.org/t/discourse-not-shown-in-iframe/26970):
>
> Aloha Communtiy, i am trying to put discourse in an iframe since i don’t have free wildcards at my dyndny hoster. I run my main apache server at port 80 and the docker containing discourse redirects to port 82. the page i am talking about is here: [http://archy.no-ip.org/forum/](http://archy.no-ip.org/forum/) instead of the discourse page at [http://archy.no-ip.org:81](http://archy.no-ip.org:81) it just shows the blank container. i first thought that it might be a problem with iframe to shown pages which don’t use port 80 or 443 but since another test …

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [2017 年 8 月 25 日午後 2:42 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/4 "2017-08-25T14:42:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [2017 年 8 月 25 日午後 2:44 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/5 "2017-08-25T14:44:41Z")

</div>

@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.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [2017 年 9 月 6 日午後 8:04 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/6 "2017-09-06T20:04:26Z")

</div>

> [@lkramer](#):
>
> \<iframe src=“./discoursesearch.php?s=\<?php echo get\_search\_query();?\>” height=“1000” width=“100%” style=“height:1000px;width:100%;”\>\</iframe\>

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

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [2017 年 9 月 8 日午後 3:29 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/7 "2017-09-08T15:29:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Francois\_Douville](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/francois_douville/32/94221_2.png) [@Francois\_Douville](https://meta.discourse.org/u/Francois_Douville)
#### Post date: [2018 年 7 月 26 日午後 10:55 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/8 "2018-07-26T22:55:21Z")

</div>

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/](https://www.yourforum.com/)”;

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [2018 年 7 月 27 日午後 2:56 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/9 "2018-07-27T14:56:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Francois\_Douville](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/francois_douville/32/94221_2.png) [@Francois\_Douville](https://meta.discourse.org/u/Francois_Douville)
#### Post date: [2018 年 7 月 27 日午後 3:24 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/10 "2018-07-27T15:24:34Z")

</div>

Thanks for the reply.

I changed my API key and URL, but this is all I see:

Search for the word “cours” in my forum: [https://forum.lepeuplier.ca/search?q=cours](https://forum.lepeuplier.ca/search?q=cours)

 ![50](https://global.discourse-cdn.com/meta/original/3X/a/7/a7d44785a62b39d1212f154ea3d9a0a66140e39d.png)

Search for the word “cours” in my test WordPress site: [https://test.lepeuplier.ca/?s=cours](https://test.lepeuplier.ca/?s=cours)

 ![53](https://global.discourse-cdn.com/meta/original/3X/3/7/3769b7e5dfa3639a83d8a18b2a8b51b1e3572369.png)

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

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [2018 年 7 月 27 日午後 6:17 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/11 "2018-07-27T18:17:22Z")

</div>

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](https://www.yourwordpresssite.com/discoursesearch.php?s=cours)

---

<div class="post-metadata">

### Author: ![Francois\_Douville](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/francois_douville/32/94221_2.png) [@Francois\_Douville](https://meta.discourse.org/u/Francois_Douville)
#### Post date: [2018 年 7 月 27 日午後 6:52 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/12 "2018-07-27T18:52:54Z")

</div>

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

![49](https://global.discourse-cdn.com/meta/original/3X/3/8/38995e93c6c89894522c5555b67aaaf12a295efd.png)

> [@lkramer](#):
>
> Can you try calling it directly like:
> 
> [https://www.yourwordpresssite.com/discoursesearch.php?s=cours](https://www.yourwordpresssite.com/discoursesearch.php?s=cours)

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

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [2018 年 7 月 27 日午後 7:51 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/13 "2018-07-27T19:51:03Z")

</div>

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](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.

---

<div class="post-metadata">

### Author: ![Francois\_Douville](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/francois_douville/32/94221_2.png) [@Francois\_Douville](https://meta.discourse.org/u/Francois_Douville)
#### Post date: [2018 年 7 月 27 日午後 8:08 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/14 "2018-07-27T20:08:33Z")

</div>

I don’t seem to have any error log

 ![50](https://global.discourse-cdn.com/meta/original/3X/8/6/86982b1adc29eee90f625d8eb00163af594b5fc6.png)

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

---

<div class="post-metadata">

### Author: ![Francois\_Douville](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/francois_douville/32/94221_2.png) [@Francois\_Douville](https://meta.discourse.org/u/Francois_Douville)
#### Post date: [2018 年 7 月 29 日午後 2:21 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/15 "2018-07-29T14:21:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [2018 年 7 月 30 日午後 6:25 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/16 "2018-07-30T18:25:58Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Francois\_Douville](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/francois_douville/32/94221_2.png) [@Francois\_Douville](https://meta.discourse.org/u/Francois_Douville)
#### Post date: [2018 年 7 月 30 日午後 8:04 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/17 "2018-07-30T20:04:51Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![davidkingham](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/davidkingham/32/119528_2.png) [@davidkingham](https://meta.discourse.org/u/davidkingham)
#### Post date: [2018 年 7 月 30 日午後 11:44 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/18 "2018-07-30T23:44:20Z")

</div>

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

But search results do not:

> **[Search Results for “sellers”](https://www.naturephotographers.network/?s=sellers)**

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

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [2018 年 7 月 31 日午後 2:27 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/19 "2018-07-31T14:27:21Z")

</div>

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.)

---

<div class="post-metadata">

### Author: ![Francois\_Douville](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/francois_douville/32/94221_2.png) [@Francois\_Douville](https://meta.discourse.org/u/Francois_Douville)
#### Post date: [2018 年 8 月 3 日午後 12:12 UTC](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782/20 "2018-08-03T12:12:14Z")

</div>

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 :

```plaintext
<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 :

 ![23](https://global.discourse-cdn.com/meta/original/3X/f/c/fc72deedd28791d3b5ef9c937efe10983822a2cd.png)

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!

[Next page](https://meta.discourse.org/t/add-forum-search-results-to-wordpress-search/68782.md?page=2)
