05 - WordPress Seearch

Describe WordPress Search Functionality

WordPress search functions like a small-scale search engine. By default, it is included in the WordPress core files. You can add it to your site by adding a Search Block or Search Widget. While the search tool is useful, it does have it's shortcomings. One of the drawbacks is that it can only scan content from the titles and bodies of your posts and pages, missing the content from widgets, comments, categories, gallery options, and tags. Overall, it's still useful for sites that have a lot of content like blogs and extensive archives.

Explain the Role of the URI When Searching

A URI is a Uniform Resource Identifier. It is a string of characters used to to identify a resource. It can be utilized to pinpoint a website, file, or a specific piece of data. In WordPress it plays a role when searching a WordPress site by defining the search context and passing parameters for more refined search results. For example, parameters included in the URI, tell important details about the search query. In ?s=search-term, search-term is what the user is looking for.

Explain How To Show the Current/Active Search Term in Search Form Field

Adding search terms in the search form field is actually simple and easy, and just requires modification of code that might already exist in your WordPress theme. For example, in this block of code to build the search bar:


                <form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
                <div>
                    <input type="text" value="<?php _e('Search'); } ?>" name="s" id="s"  
                        onfocus="if(this.value == '<?php _e('Search'); ?>'){ this.value = ''; }"
                        onblur="if(this.value == ''){ this.value = '<?php _e('Search'); ?>'; }"/>
                </div>
                </form>
            

This line of code:

value="<?php _e('Search'); } ?>"

Needs to be updated to this:

value="<?php if( is_search() ){ echo get_search_query(); } else { _e('Search'); } ?>" 

The updated line of code will echo out the search query instead of leaving it blank due to the echo get_search_query(); function nested inside of the code that builds the search form.

Summary of the Documentation

The functionality of search in WordPress works similarly to a small search engine that scans your WordPress site for content that matches search terms and queries. While it's come a long way, it still doesn't scan all of yours sites content, the information located in widgets, comments, categories, tags, etc. The URI can be useful when saerching because it provides context and parameters to what's being searched. There also exist functions in WordPress that modify search functionality, one of them being the capability to show search terms in the search field after the search was conducted. Overall, WordPress comes with a plethora of ways to enhance the searching experience for their users, and the things listed in this document are just a few of many.