Extend autocomplete for tags

ForestForTrees

Well-known member
Currently, when you are adding tags, autocomplete doesn't include many tags that might be useful to the user. For example, if you have a tag called "migraines and headaches," it won't appear if the user starts typing headaches. Apparently, it only looks for exact, from-the-start, matches.

When members use existing tags rather than creating new tags, the tagging system is less fragmented and more useful. As a result, users will create new tags rather than using existing tags. This makes the tagging system less useful and decreases the SEO benefit because of thin content.

If the right hook exists, it should be pretty simple to write an addon that would broaden the search called by autocomplete. Specifically, it would return any tag where a word in the user's string matches any substring of an existing tag.

autocomplete-gif.127975
 
If you're open to doing a small code edit, you may not need an add on at all.

Haven't got the code in front of me right now but it's literally a case of changing one line of code from "r" to "lr".

It would then search the database like "%tag%" instead of "tag%".

Don't normally condone code edits but with it being such a simple one it might not hurt. Conversely it should be relatively simple to create an add on for it but it may well actually need to overwrite the existing function which may have an adverse effect if other add ons try to extend the same code.
 
Last edited:
In /library/XenForo/Model/Tag.php this is the function Chris is referring to:

PHP:
public function autoCompleteTag($tag, $limit = 10)
    {
        return $this->fetchAllKeyed($this->limitQueryResults(
            "
                SELECT *
                FROM xf_tag
                WHERE tag LIKE " . XenForo_Db::quoteLike($tag, 'r') . "
                    AND (use_count > 0 OR permanent = 1)
                ORDER BY tag
            ", $limit
        ), 'tag_id');
    }

For an add-on to do this, the entire function has to be overwritten otherwise you can change the bolded r to "lr" like Chris mentioned.


[edit] I just realized you can't bold within php tags. Change "::quoteLike($tag, 'r')" to "::quoteLike($tag, 'lr')".
 
Last edited:
Back
Top Bottom