Replace WordPress Search With Google Custom Search
-
I am trying to create a global search for a multisite website without having to use the sidebar or a plugin. I would like to replace the WordPress search with a Google Custom Search. I’ve tried to talk to others who have succeeded at this, but I am using a custom theme and it is making it extremely difficult to solve this. I am not really experienced in PHP, but I am trying to do the best I can.
search.php code:
<?php $searchQuery = esc_attr($_GET['s']); $postTypeQuery = get_query_var('post_type'); ?> <?php get_header(); ?> <div class="wrapper wrapper_push"> <!-- MAIN --> <div class="main" role="main"> <div class="main-section"> <div class="hdgBar"> <h1 class="hdg hdg_h1"> <?php if ($searchQuery) : ?> Search: <?php echo $searchQuery; ?> <?php else : ?> No Results Found <?php endif; ?> </h1> </div> <div class="main-content"> <div class="grid"> <!-- Post List --> <div class="grid-col grid-col_9of12"> <?php if (!have_posts() || !$searchQuery) : ?> <?php if ($searchQuery) : ?> <p>No posts were found for that search term.</p> <?php else : ?> <p>Please enter terms to search for.</p> <?php endif; ?> <?php if ('post' !== $postTypeQuery && !is_home()) : ?> <!-- Search --> <fieldset class="searchSideBar" role="search"> <form action="<?php bloginfo('home'); ?>/"> <input type="input" name="s" placeholder="Search" class="input input_sidebar" /> <?php if ('post' === $postTypeQuery || is_home()) : ?> <input type="hidden" value="post" name="post_type" id="post" /> <?php endif; ?> <input type="submit" class="search-mag search-mag_red" value=""/> </form> </fieldset> <?php endif; ?> <?php else : ?> <ul class="mediaGroup"> <?php $i = 0; ?> <?php if (have_posts()) : while (have_posts()) : the_post(); $i++; ?> <?php ($i > 5) ? $class = ' hidden' : $class = ''; ?> <li class="postList-item<?php echo $class; ?>"> <div class="media"> <div class="media-img"> <?php if (has_post_thumbnail()) : ?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> <?php the_post_thumbnail('120x120'); ?> </a> <?php else : ?> <div class="placeholder placeholder_categoryListItem"> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> <img class="placeholder-img_categoryListItem" src="<?php echo THEME_PATH_URL; ?>/assets/images/placeholder-logo.png" /> </a> </div> <?php endif; ?> </div> <div class="media-bd media-bd_inline"> <div class="fullView"> <?php $cats = wp_get_post_categories(get_the_ID()); ?> <?php if (count($cats)) : ?> <ul class="tag tag_push"> <?php foreach ($cats as $catId) : ?> <?php $category = get_category($catId); ?> <li> <a href="<?php echo get_category_link($category->cat_ID); ?>" class="tag-link"> <?php echo $category->name; ?> </a> </li> <?php endforeach; ?> </ul> <?php endif; ?> </div> <div class="hdgBar hdgBar_thin"> <h2 class="hdg hdg_h2"> <a href="<?php the_permalink(); ?>" class="hdg hdg_h2"> <?php the_title(); ?> </a> </h2> </div> <div class="fullView"> <?php $excerpt = get_the_excerpt(); if (strlen($excerpt) > 180) { $excerpt = substr($excerpt, 0, 180) . '...'; } echo $excerpt; ?> </div> </div> </div> </li> <?php endwhile;endif; ?> </ul> <div class="loadMore hidden"> <a href="#" class="btn btn_full">Load More</a> </div> <?php endif; ?> </div> <?php if ($searchQuery && ('post' === $postTypeQuery || is_home())) { get_template_part('views/sidebar', 'search'); } ?> </div> </div> </div> </div> </div> <script> jQuery(document).ready(function($) { var $posts = $('.postList-item'); var $loadMoreBtn = $('.loadMore'); var postLimitIndex = 5; if ($posts.length > 5) { $loadMoreBtn.removeClass('hidden'); } // Show more posts when load more btn clicked $loadMoreBtn.on('click', function(e) { e.preventDefault(); postLimitIndex += 5; $posts .filter(function(index) { if (index < postLimitIndex) { if ($(this).hasClass('hidden')) { return true; } } }) .hide() .removeClass('hidden') .fadeIn(1500); if ($posts.length <= postLimitIndex) { $loadMoreBtn.hide(); } }); }); </script> <?php get_footer(); ?>The current search result page looks great, but I need to be able to include items from multiple sites within a WordPress multisite setup. I would like to keep the look the same, but show the results from all sites.
I created the google custom search, I have the ID and code but I do not know how to place it in the search.php in order to make it work correctly. Any suggestions would be extremely helpful.
The topic ‘Replace WordPress Search With Google Custom Search’ is closed to new replies.