Link to home
Start Free TrialLog in
Avatar of Brad Bansner
Brad Bansner

asked on

Replace Google Custom Search Engine with default WordPress search

The file content-search.php on my WordPress site has this content:

<div class="row">
	<div class="span12">

<h1>SEARCH RESULTS</h1>

<script>
  (function() {
    var cx = 'xxxxxxxxx';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//cse.google.com/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
</script>
<gcse:searchresults-only></gcse:searchresults-only>

	</div>
</div>

Open in new window


I would like to simply replace this with the built-in (default) WordPress search feature. The theme I am working with is custom and was created by someone else. Is there some kind of default PHP code I can replace the above script with, to use the default WordPress search instead? The theme doesn't have a template file for search, other than what is shown above.
Avatar of Andrew Derse
Andrew Derse
Flag of United States of America image

Tough one to answer if the default searchform.php is missing.

You should be able to call: get_search_form( true ) and have it populate if you have that form.

If you don't have a searchform.php, try this:
<?php
/**
 * Template for displaying search forms in Twenty Seventeen
 *
 * @package WordPress
 * @subpackage Twenty_Seventeen
 * @since 1.0
 * @version 1.0
 */

?>

<?php $unique_id = esc_attr( uniqid( 'search-form-' ) ); ?>

<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
	<label for="<?php echo $unique_id; ?>">
		<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label', 'twentyseventeen' ); ?></span>
	</label>
	<input type="search" id="<?php echo $unique_id; ?>" class="search-field" placeholder="<?php echo esc_attr_x( 'Search &hellip;', 'placeholder', 'twentyseventeen' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />
	<button type="submit" class="search-submit"><?php echo twentyseventeen_get_svg( array( 'icon' => 'search' ) ); ?><span class="screen-reader-text"><?php echo _x( 'Search', 'submit button', 'twentyseventeen' ); ?></span></button>
</form>

Open in new window

Avatar of Brad Bansner
Brad Bansner

ASKER

I actually do have the file searchform.php in the theme directory, but this is all that is inside it:

<?php
/**
 * Search Form for the theme.
 *
 * @package bootstart
 */
?>
<form role="search" method="get" id="searchform" class="searchform" action="https://www.readingthermal.com/search-results.html">
        <div class="<?php bstart_class('search-field-div', array('form-group') ); ?>">
            <input type="text" class="form-control" value="" name="q" id="s" placeholder="SEARCH OUR SITE">
            <input type="submit" id="searchsubmit" class="btn btn-primary" value="GO">
        </div>
</form>

Open in new window

The problem is that the search results page has been overwritten with the Google Custom Search code.
ASKER CERTIFIED SOLUTION
Avatar of Andrew Derse
Andrew Derse
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I get an error message when trying that code:

Fatal error: Call to undefined function twentyseventeen_get_svg() in /home/readingt/public_html/wp-content/themes/rt-website/content-search.php on line 42

This is line 42:

'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',

Open in new window