Add forum search results to WordPress search

This has to be placed into your search.php file. <?php echo urlencode(get_search_query());?> is PHP code and you can’t execute that from inside your page builder — you’ll need to write a shortcode to access that value that if you want to integrate it in your theme like this.

Thanks.

This is my search.php file from my theme : search_GeneratePress.php - Google Drive

Where do I place the iframe code? I tried to put it in the end, but when I go back to customize my search page with my page builder, I don’t know what to do to find this new section…

It’s hard to tell from this template alone — your search page appears to be the one you customised in Elementor, so I can’t correlate this markup with the current layout. However, the template offers you a couple of action hooks; generate_before_main_content and generate_after_main_content which I’d personally use to inject the iframe. If you’re not comfortable with WordPress hooks though, it’s fine to modify the template directly.

This is the standard search.php file from GeneratePress. I just added the iframe code. What I don’t understand is that I need to customize my search page with my page builder so even if I add the code in my theme search file, I will not see it…

Also, I’m not a developer so it’s normal I don’t understand anything hehe

OK, so now I see you have reverted to the default search template and your iframe is loaded in the page correctly, but discoursesearch.php isn’t returning any results. If you’ve confirmed with your hosts that the necessary libraries are available, the next most obvious cause would be that you haven’t configured your API settings correctly, so recheck your API key and base URL values.

1 Like

As I mentioned earlier, if you want to use the customised version of your search page, you’ll need to get a shortcode written that will let you insert the iframe from your page builder

Ok it is working with my default search page from my theme! My URL was with www but I removed it.

So now I have to create a shortcode to be able to customize this page with my page builder (Elementor). I really don’t know how to create it. I checked your link from Wordpress

1 Like

If you’re not familiar with PHP development, it might be a challenge to implement a shortcode yourself. I may be able to assist you, PM me if you’d like to discuss further.

3 Likes

Try these steps:

  1. Install and activate the Insert PHP Code Snippet plugin
  2. Navigate to the plugin settings in admin (look for the “PHP” logo in the sidebar) and add a new snippet
  3. Set the “Tracking Name” field to something like search-query and the “PHP Code” field to <?php echo urlencode(get_search_query());
  4. Save the snippet
  5. Edit your custom search page in Elementor and replace <?php echo urlencode(get_search_query());?> with [xyz-ips snippet="search-query"] (replace “search-query” with the appropriate tracking name if you saved it with a different one)

Let me know how that works for you

3 Likes

It still doesn’t work. I completed the plugin part :

And I see this URL in Elementor https://forum.lepeuplier.ca/search?q=[xyz-ips%20snippet=

The HTML widget won’t render shortcode output while you’re editing, you’ll have to save the page to see the actual markup

1 Like

This is what I did. You can test it live https://test.lepeuplier.ca and write “cours”

OK, I see what you mean. So it appears you’ve switched from a HTML widget to a shortcode widget?

To verify where the issue lies, can you remove all the markup but the shortcode itself from the widget? I also noticed there is a leading space character in the snippet attribute of your shortcode. I’d assume the parsing would trim that, but I’d remove it just to be sure.

If it’s executed correctly, then when you load the search page, you should just see the search term rendered to the page, which means there’s some issue in how the widget is processing it. If you only see the shortcode, then the shortcode itself isn’t executing correctly.

1 Like

Yes I tested it with a HTML widget, it does the same result.

I tested only with the shortcode and I only see the search term. So it means that there’s some issue in how the widget is processing it?

Yes, that’s correct. I don’t know how the shortcode widget handles regular markup as it’s only intended to process a shortcode directly. The HTML widget should also execute any shortcodes present, but it might be confused due to the shortcode being wrapped inside an HTML attribute. In either case, I think the easiest solution is to put the entire iframe markup inside the shortcode, which should allow you to render it in either widget.

So edit the PHP Code field for your snippet to this:

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

Whether you’re using the HTML or Shortcode widget, remove everything but the shortcode itself and test again.

P.S.
The above code is untested, but fingers crossed it will work first time. :sweat_smile:

2 Likes

YESSS! It is working haha thanks so much! :smiley:

How can I change CSS settings (font-size, font-family and the avatar in circle)?

1 Like

Try replacing the code in discoursesearch.php with the following:

<?php

define('WP_USE_THEMES', false);
require_once('./wp-load.php');
?>
<!DOCTYPE html>
<html>
<head>
<style>
/* Your styles go here */
</style>
</head>
<body>
<?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>';
}
?>
</body>
</html>

Again, this code is untested, so do make sure you backup before editing. These changes should hopefully allow you to add your own CSS rules directly in the file, whereas the original code would be more difficult to add your own styles to.

3 Likes

This is perfect! Thanks again for your time, it is so appreciated! You made a huge difference for my site :smiley:

3 Likes

It is necessary to filter the variable. Otherwise can be:

search.php?s=<script>alert('xss');</script>

I have a question. Why gives only 5 results?

I just now decided to try to do it on another site.

1 Like