News:

Wondering if this will always be free?  See why free is better.

Main Menu

Adding custom search filters to 2.1 Search

Started by Wellwisher, February 20, 2021, 05:26:26 PM

Previous topic - Next topic

Wellwisher

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

Wellwisher

If it helps, the following code searches for Topic Titles only:



<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


live627

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

Wellwisher

Quote from: live627 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

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.

live627


<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

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;
});
});

Wellwisher

#5
Quote from: live627 on February 22, 2021, 03:51:58 AM

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:


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



Replace with:



<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


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

Speed King

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  ???

shadav

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

Wellwisher

Quote from: Speed King on February 23, 2021, 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  ???

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.


Speed King

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.

Wellwisher

#10
Quote from: Speed King on February 23, 2021, 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.

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:



// 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:



// 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>';




Speed King


Wellwisher

Hello again,

Requesting further help folks.  :P

I figured out the code needed to search in "topic replies" (code shown below).
However, when I add it after the code @live627 provided for index.template.php, it breaks the search drop down options (shown in image attached). Also index.template.php is attached too.

Not sure how to fix this issue.  ???

Appreciate any help as always.





// Search in Topic Replies only

echo '
<option value="message_only"', ($selected == 'search_selection' ? '' : ''), '>','Topic Replies', '</option>
<input type="hidden" name="message_only" value="0" /></select>';


Kindred

any additional options must be added BEFORE the closing </select>
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Advertisement: