Advertisement:

Author Topic: Adding custom search filters to 2.1 Search  (Read 353 times)

Offline Wellwisher

  • Full Member
  • ***
  • Posts: 447
  • Gender: Male
Adding custom search filters to 2.1 Search
« on: February 20, 2021, 05:26:26 PM »
Hello everyone,

I am trying to add new search filters to the default 2.1 search theme and need help adding these:

- "Search by topic titles only"
- "Search by topic responses only"
- "Try the Advance search" <- When the user selects this option, it redirects them to the main search page located here: MySite.com/forum/index.php?action=search2

Example shown in image attached:



Why? Some users may only want to search and then follow main topics. Others may want to search a term in messages only. I am also slowly converting my 2.0 theme to 2.1 theme and I am enhancing my forum based on feedback from members. :)

Offline Wellwisher

  • Full Member
  • ***
  • Posts: 447
  • Gender: Male
Re: Adding custom search filters to 2.1 Search
« Reply #1 on: February 21, 2021, 08:20:11 PM »
If it helps, the following code searches for Topic Titles only:

Code: [Select]


<option value="subject_only"', ($selected == 'all' ? '' : ''), '>', 'Topic titles', '</option>
<input type="hidden" name="subject_only" value="1" />


However when I add this code to index.template.php, it limits the "Entire search" to topic titles too.

Attached my index.template.php

Test bench details:
SMF version 2.1 RC3
Local host/ WAMP set-up


Offline live627

  • Developer
  • SMF Hero
  • *
  • Posts: 5,869
  • Gender: Male
    • live627 on Facebook
    • live627 on GitHub
    • live627 on LinkedIn
    • @live627 on Twitter
    • livemods
Re: Adding custom search filters to 2.1 Search
« Reply #2 on: February 21, 2021, 11:02:11 PM »
You may find with the browser's dev tools that the hidden input always sends a value. With that knowledge, you can add some JavaScript to add it

Offline Wellwisher

  • Full Member
  • ***
  • Posts: 447
  • Gender: Male
Re: Adding custom search filters to 2.1 Search
« Reply #3 on: February 22, 2021, 12:20:41 AM »
You may find with the browser's dev tools that the hidden input always sends a value. With that knowledge, you can add some JavaScript to add it

My knowledge is pretty basic in JS and PHP. I can read it and get a basic idea of what the code does but I can't construct it. I can copy & paste, amend values but the rest is basically trial & error.  :laugh:

Going back to what you mentioned about hidden inputs, I was reading the index.template.php and it does have custom hidden inputs for search queries for  'current_topic' & 'current_board' hence that led me to believe it's something that can be done without using JS.

Offline live627

  • Developer
  • SMF Hero
  • *
  • Posts: 5,869
  • Gender: Male
    • live627 on Facebook
    • live627 on GitHub
    • live627 on LinkedIn
    • @live627 on Twitter
    • livemods
Re: Adding custom search filters to 2.1 Search
« Reply #4 on: February 22, 2021, 03:51:58 AM »
Code: [Select]
<option value="subject_only"', ($selected == 'search_selection' ? '' : ''), '>','Topic Titles', '</option>
<input type="hidden" name="subject_only" value="0" />
if it's 0, the search area ignores it

JavaScript to add at the end off theme.js
Code: [Select]
window.addEventListener("DOMContentLoaded", function()
{
document.forms.search_form.addEventListener('submit', function()
{
let
el = this.search_selection,
val = el.options[el.selectedIndex].value;
this.subject_only.value = val !== 'subject_only' ? 0 : 1;
});
});

Offline Wellwisher

  • Full Member
  • ***
  • Posts: 447
  • Gender: Male
Re: Adding custom search filters to 2.1 Search
« Reply #5 on: February 22, 2021, 10:11:53 PM »

if it's 0, the search area ignores it


This is what they don't explain on Youtube videos. Thank you live627 works perfectly!  8). I think SMF 2.1 NEEEEEDS to add this search feature. It will save the server having to search the entire forum and focus its search on topic titles.  ???




For my future reference  8):

1. Find in theme file index.template.php:

Code: [Select]
<option value="members"', ($selected == 'members' ? ' selected' : ''), '>', $txt['search_members'], ' </option>';


Replace with:

Code: [Select]

<option value="members"', ($selected == 'members' ? ' selected' : ''), '>', $txt['search_members'], ' </option>
<option value="subject_only"', ($selected == 'search_selection' ? '' : ''), '>','Topic Titles', '</option>
<input type="hidden" name="subject_only" value="0" />';



2. Add JavaScript at the end off theme/scripts/theme.js

Code: [Select]
window.addEventListener("DOMContentLoaded", function()
{
document.forms.search_form.addEventListener('submit', function()
{
let
el = this.search_selection,
val = el.options[el.selectedIndex].value;
this.subject_only.value = val !== 'subject_only' ? 0 : 1;
});
});

I will now see if I can reverse engineer your code to work on searching in "topic responses". Wish me luck my dude. Thank you again.  :-X
« Last Edit: February 22, 2021, 10:26:07 PM by Wellwisher »

Offline Speed King

  • Newbie
  • *
  • Posts: 6
Re: Adding custom search filters to 2.1 Search
« Reply #6 on: Yesterday at 12:28:14 AM »
I have tested this script in my forum and it's very useful, but it's not available for guests. Only registered users can see "Topic Titles" option in Quick search field  ???

Online shadav

  • Support Specialist
  • Sophist Member
  • *
  • Posts: 1,043
  • Gender: Female
    • shadav on Facebook
    • leashadav on GitHub
    • Ask The Muslims: A Place To Ask Honest Questions and Receive Respectful Answers
Re: Adding custom search filters to 2.1 Search
« Reply #7 on: Yesterday at 12:33:08 AM »
it's not for 2.1 but you could probably look at the code for the search focus dropdown mod and see how it works and maybe figure out how to get 2.1's search box to do what you want
https://custom.simplemachines.org/mods/index.php?mod=1634

Offline Wellwisher

  • Full Member
  • ***
  • Posts: 447
  • Gender: Male
Re: Adding custom search filters to 2.1 Search
« Reply #8 on: Yesterday at 01:09:18 AM »
I have tested this script in my forum and it's very useful, but it's not available for guests. Only registered users can see "Topic Titles" option in Quick search field  ???

Mine works for guests:

Some custom themes in their index.template.php file may limit the search options e.g "If user is logged in, show search options x,y,z" or it can also be a permissions option for guests you need to enable on your theme/ forum.


Offline Speed King

  • Newbie
  • *
  • Posts: 6
Re: Adding custom search filters to 2.1 Search
« Reply #9 on: Yesterday at 01:52:58 AM »
I use SMF default theme. Guests can search the forum, but they cannot see the memberlist.

If I allow guests to see the memberlist, then they can see and the "Topic Titles" option in Quick Search, else they see "Entire Forum" only.

Offline Wellwisher

  • Full Member
  • ***
  • Posts: 447
  • Gender: Male
Re: Adding custom search filters to 2.1 Search
« Reply #10 on: Yesterday at 02:07:32 AM »
I use SMF default theme. Guests can search the forum, but they cannot see the memberlist.

If I allow guests to see the memberlist, then they can see and the "Topic Titles" option in Quick Search, else they see "Entire Forum" only.

Ah that was my bad, *possibly* (most definitely).  ;D

Try this, undo any chances you made in the index.template.php file (apart from the Javascript code you added in theme.js).


In index.template.php file:

Find:

Code: [Select]

// Can't search for members if we can't see the memberlist
if (!empty($context['allow_memberlist']))
echo '
<option value="members"', ($selected == 'members' ? ' selected' : ''), '>', $txt['search_members'], ' </option>';


Add after:

Code: [Select]

// Search for topic titles only - live627
echo '<option value="subject_only"', ($selected == 'search_selection' ? '' : ''), '>','Topic Titles', '</option>
  <input type="hidden" name="subject_only" value="0" />
</select>';


« Last Edit: Yesterday at 02:21:26 AM by Wellwisher »

Offline Speed King

  • Newbie
  • *
  • Posts: 6
Re: Adding custom search filters to 2.1 Search
« Reply #11 on: Yesterday at 06:48:45 AM »
Thanks, now everything is OK :)