• Resolved mickerlodeon

    (@mickerlodeon)


    When searching for “AT&T” on my blog (https://mickerlodeon.com) I come up with no results. But, when I search for “AT& amp;T” I get the results I should. Is there anyway of replacing “&” with “& amp;” when someone searches?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I was having the same difficulties. I tried every search plugin out there to no avail.

    I added some debugging and dug and dug until I realized the problem was that the ampersand was never being sent to the PHP from the search form.

    Finally I figured it out by hacking in some javascript to my theme’s search form.

    Note: All this does is change the “&” in a search query to “& amp;” (no spaces) before it is submitted.

    WP’s default search engine still does not search successfully when this is done. I am using the Search Unleashed plugin with the “Lucene” search engine (remember to re-index!”). This is the only search engine in the Search Unleashed plugin that works, but other search engines in other plugins may work as well. To test, search for your string with & amp; (no spaces) in it (eg: “AT& amp;T”). If that works and finds AT&T, then this change will allow your users to enter only “AT&T”.

    How To:

    In the tag for the search form you will need to add the following onsubmit attribute:

    onSubmit="s.value = s.value.replace('&','& amp; amp;'); submit();"
    ** No spaces in the “& amp; amp;”! **

    Where the “s” from “s.value” is the id of your search input text element.

    My form looks like this now:

    <form method="get" id="searchform" action="<?php bloginfo('home'); ?>" onSubmit="s.value = s.value.replace('&','& amp; amp;'); submit();" />
    <div><input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
    <input type="submit" class="button" id="searchsubmit" value="Search" />
    </div>
    </form>

    Note: This isn’t a perfect fix. For example, the Search Unleashed’s landing page search box will not quite work. If you searched for AT&T, it will show ‘you landed here by searching for “AT”, instead of “AT&T”. You can turn that landing page notice off though.

    Hopefully this helps you. It was a dealbreaker for me and I sweated the solution for far longer than it should have taken. I’d love to see WP handle this more elegantly.

    Thread Starter mickerlodeon

    (@mickerlodeon)

    This does work for me, even with WordPress’ built in search. Just adding the onSubmit attribute solved the issue for me, searching for AT&T now brings up the results that it should.

    When writing ampersands in WordPress’ post title box I still have to add the “amp;” otherwise my pages don’t validate. WordPress should really deal with that for me.

    And, after hitting enter to submit the search form “AT&T” turns into “AT& amp;T” (no spaces) before the page reloads. This isn’t a perfect solution, because a perfect solution would be for WordPress to deal with this natively. But, at least it works now.

    @mrtemple Thanks for all your hard work, I’ve been searching for a solution to this problem for a couple of months now and haven’t even found any discussion of it. I’ve found the same problem on other WordPress sites and it’s almost as if no one knows that the problem exists.

    Thread Starter mickerlodeon

    (@mickerlodeon)

    After running my site through an HTML validator it was showing a few errors.

    Adding “amp;” after the first ampersand inside the onsubmit attribute and changing “onSubmit” to “onsubmit” fixed all of them.

    Here’s the new code:

    onsubmit=”s.value = s.value.replace(‘& amp;’,’& amp; amp;’); submit();”

    (no spaces before the “amp;”s)

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Searching with Ampersands’ is closed to new replies.