Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Ben Taylor
Ben Taylor
902 Points

Editing my Search

Hey guys,

I've made a WooCommerce shop for someone but they have a lot of products with similar wording in them, so the search functionality isn't working great. However, if you put quotes around your search term, it works a lot better.

I want to dive into the PHP and edit it so when someone searches it actually adds quotes around the search term they've typed in, does anyone know how to do this?

Thanks so much!

Ben

4 Answers

Andrew McCormick
Andrew McCormick
17,730 Points

See: http://docs.woothemes.com/document/overriding-the-product-search-box-widget/

Seems like you would create a new 'product-serachform.php' file and change the value of the input to use single quotes and then place your double quotes around your get_search_query(); . I'm not 100% on this, but seems like it should work. See below for example

<form role="search" method="get" id="searchform" action="<?php echo esc_url( home_url( '/'  ) ); ?>">
            <div>
                <label class="screen-reader-text" for="s"><?php _e( 'Search for:', 'woocommerce' ); ?></label>
                <input type="text" value='"<?php echo get_search_query(); ?>"' name="s" id="s" placeholder="<?php _e( 'Search for products', 'woocommerce' ); ?>" />
                <input type="submit" id="searchsubmit" value="<?php echo esc_attr__( 'Search' ); ?>" />
                <input type="hidden" name="post_type" value="product" />
            </div>
        </form>
Ben Taylor
Ben Taylor
902 Points

Hi Andrew,

Thanks for the reply.. I did this, however this only adds quote marks into the input box, which I don't want.. I basically want the quote marks to only be in the code, so to the user it looks like they're searching as normal, but the PHP adds quote marks to it once they submit it.. Also, it only changes it on the search widget, not the search bar at the top of the page ( http://www.diablomagic.co.uk ).. Any other ideas? :)

Thanks,

Ben

Andrew McCormick
Andrew McCormick
17,730 Points

true. So i guess we would want to track down the function that the form POSTs to and add the quotes when it gets the $_POST

Ben Taylor
Ben Taylor
902 Points

...and how would we do that? :P

Andrew McCormick
Andrew McCormick
17,730 Points

a lot of searching through woocommerce code :) search for where product-serachform.php is called and inserted and see what function is being called to process it. It's probably in multiple pages. You may be able to hook in to it, but sometimes I find that I just have to change the core woocommerce file. If I do change the core file, I make notes of it in a text file I keep in my theme, so when I update the plugin I know what I need to go and change.

Andrew McCormick
Andrew McCormick
17,730 Points

since it's posting to the home_url , then we would go back to the beginning of the wordpress loop and track it down. Search results are an archive type so track how woocommerce process archive product types.
-NOTE- I do not know that actual answer, I'm just trying to help you track it down. I've done a fair amount of woocommerce customizing but haven't done this. So if this is obvious stuff to you, let me know and I'll stop. If I have time later today I will try to see if I can track it down. If no one answers first

Ben Taylor
Ben Taylor
902 Points

Hi Andrew,

I'm more of a designer than a programmer, I know the bare bones of PHP so I think this is a little beyond my scope.. If you did have time later that'd be absolutely amazing and much appreciated :)

Andrew McCormick
Andrew McCormick
17,730 Points

Ben, Ok. So I rummaged through Woocommerce, which I found out it just uses the basic WP searching. So then through WP codex and the core to find that it's really broken at the root of WP which doesn't have the best search capabilities built in. The best option I could find (especially since you are more a designer) is to use a better search plugin with woocommerce. Specifically I read lots of love for Relevanssi. Plugin Repository Page: https://wordpress.org/plugins/relevanssi/ Relevanssi with WooCommerce: http://wordpress.org/support/topic/relevanssi-and-woocommerce'

hope that helps.