Skip to content

Instantly share code, notes, and snippets.

@srikat
Last active February 24, 2017 23:30
Show Gist options
  • Save srikat/560a12bc1f666a325204 to your computer and use it in GitHub Desktop.
Save srikat/560a12bc1f666a325204 to your computer and use it in GitHub Desktop.
How to add Google Custom Search in Genesis. https://sridharkatakam.com/add-google-custom-search-genesis/
add_filter( 'genesis_search_text', 'custom_search_text' );
/**
* Customizes the text of the search input
*
* @param string $text current text
* @return string modified text
*/
function custom_search_text( $text ) {
return esc_attr( 'Google Search...' );
}
add_filter( 'genesis_search_form', 'custom_search_form', 10, 4 );
function custom_search_form( $form, $search_text, $button_text, $label ) {
$search_text = get_search_query() ? apply_filters( 'the_search_query', get_search_query() ) : apply_filters( 'genesis_search_text', __( 'Search this website', 'genesis' ) . ' …' );
$button_text = apply_filters( 'genesis_search_button_text', esc_attr__( 'Search', 'genesis' ) );
$onfocus = "if ('" . esc_js( $search_text ) . "' === this.value) {this.value = '';}";
$onblur = "if ('' === this.value) {this.value = '" . esc_js( $search_text ) . "';}";
//* Empty label, by default. Filterable.
$label = apply_filters( 'genesis_search_form_label', '' );
$value_or_placeholder = ( get_search_query() == '' ) ? 'placeholder' : 'value';
if ( genesis_html5() ) {
$form = sprintf( '<form %s>', genesis_attr( 'search-form' ) );
if ( genesis_a11y( 'search-form' ) ) {
if ( '' == $label ) {
$label = apply_filters( 'genesis_search_text', __( 'Search this website', 'genesis' ) );
}
$form_id = uniqid( 'searchform-' );
$form .= sprintf(
'<meta itemprop="target" content="%s"/><label class="search-form-label screen-reader-text" for="%s">%s</label><input itemprop="query-input" type="search" name="q" id="%s" %s="%s" /><input type="submit" value="%s" /></form>',
home_url( '/?s={s}' ),
esc_attr( $form_id ),
esc_html( $label ),
esc_attr( $form_id ),
$value_or_placeholder,
esc_attr( $search_text ),
esc_attr( $button_text )
);
} else {
$form .= sprintf(
'%s<meta itemprop="target" content="%s"/><input itemprop="query-input" type="search" name="q" %s="%s" /><input type="submit" value="%s" /></form>',
esc_html( $label ),
home_url( '/?s={s}' ),
$value_or_placeholder,
esc_attr( $search_text ),
esc_attr( $button_text )
);
}
} else {
$form = sprintf(
'<form method="get" class="searchform search-form" action="%s" role="search" >%s<input type="text" value="%s" name="q" class="s search-input" onfocus="%s" onblur="%s" /><input type="submit" class="searchsubmit search-submit" value="%s" /></form>',
// home_url( '/' ),
home_url( '/search/' ),
esc_html( $label ),
esc_attr( $search_text ),
esc_attr( $onfocus ),
esc_attr( $onblur ),
esc_attr( $button_text )
);
}
return $form;
}
add_filter( 'genesis_attr_search-form', 'custom_attributes_search_form' );
/**
* Add attributes for search form.
*
* @since 2.2.0
*
* @param array $attributes Existing attributes.
*
* @return array Amended attributes.
*/
function custom_attributes_search_form( $attributes ) {
$attributes['action'] = home_url( '/search/' );
return $attributes;
}
.entry-content .search-form {
width: 100%;
max-width: 800px;
}
.search-form input {
float: left;
font-size: 16px;
}
.content .search-form input[type="search"] {
width: 80%;
margin-right: 2%;
}
.search-form input[type="submit"] {
width: 18%;
margin-top: 0;
}
#cse .gsc-control-cse {
padding: 0;
border: none;
}
#cse .gsc-above-wrapper-area {
border-bottom: none;
padding: 0;
}
#cse table {
line-height: 1;
margin-bottom: 0;
}
#cse tbody {
border-bottom: none;
}
#cse .gsc-selected-option-container {
max-width: none;
}
#cse .gsc-result .gs-title {
height: auto;
}
@media only screen and (max-width: 661px) {
.search-form input {
float: none;
}
.search-form input[type="search"] {
width: 100%;
margin-right: 0;
margin-bottom: 10px;
}
.search-form input[type="submit"] {
width: auto;
}
}
<?php
/*
Template Name: Google Custom Search
Description: Google Custom Search
Created by John Levandowski
Modified by: Rowell Dionicio and Sridhar Katakam
Site: https://rowell.dionicio.net/adding-google-custom-search-to-genesis-framework/, https://sridharkatakam.com/add-google-custom-search-genesis/
*/
# Force full width content
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
# Remove the breadcrumb
// remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
# Remove the post content
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
# Add custom post content
add_action( 'genesis_entry_content', 'custom_do_post_content' );
/**
* Outputs custom post content
*
* @return void
*/
function custom_do_post_content() {
get_search_form(); ?>
INSERT GOOGLE CUSTOM SEARCH CODE HERE
<?php }
genesis();
<?php
/*
Template Name: Google Custom Search
Description: Google Custom Search
Created by John Levandowski
Modified by: Rowell Dionicio and Sridhar Katakam
Site: https://rowell.dionicio.net/adding-google-custom-search-to-genesis-framework/, https://sridharkatakam.com/add-google-custom-search-genesis/
*/
# Force full width content
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
# Remove the breadcrumb
// remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
# Remove the post content
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
# Add custom post content
add_action( 'genesis_entry_content', 'custom_do_post_content' );
/**
* Outputs custom post content
*
* @return void
*/
function custom_do_post_content() {
get_search_form(); ?>
<div id='cse' style='width: 100%;'>Loading</div>
<script src='//www.google.com/jsapi' type='text/javascript'></script>
<script type='text/javascript'>
google.load('search', '1', {language: 'en', style: google.loader.themes.V2_DEFAULT});
google.setOnLoadCallback(function() {
var customSearchOptions = {};
var orderByOptions = {};
orderByOptions['keys'] = [{label: 'Relevance', key: ''} , {label: 'Date', key: 'date'}];
customSearchOptions['enableOrderBy'] = true;
customSearchOptions['orderByOptions'] = orderByOptions;
var customSearchControl = new google.search.CustomSearchControl('003698980430458114438:pxccgvs_fv0', customSearchOptions);
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
var options = new google.search.DrawOptions();
options.enableSearchResultsOnly();
options.setAutoComplete(true);
customSearchControl.draw('cse', options);
function parseParamsFromUrl() {
var params = {};
var parts = window.location.search.substr(1).split('&');
for (var i = 0; i < parts.length; i++) {
var keyValuePair = parts[i].split('=');
var key = decodeURIComponent(keyValuePair[0]);
params[key] = keyValuePair[1] ?
decodeURIComponent(keyValuePair[1].replace(/\+/g, ' ')) :
keyValuePair[1];
}
return params;
}
var urlParams = parseParamsFromUrl();
var queryParamName = 'q';
if (urlParams[queryParamName]) {
customSearchControl.execute(urlParams[queryParamName]);
}
}, true);
</script>
<?php }
genesis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment