Last active
November 19, 2016 13:50
-
-
Save rickrduncan/6927390 to your computer and use it in GitHub Desktop.
Google CSE for WordPress/Genesis Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Template Name: Google CSE | |
* | |
* This file adds the Google SERP template to our Genesis Child Theme. | |
* | |
* @author Rick R. Duncan | |
* @link http://rickrduncan.com | |
* @license http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later) | |
* | |
*/ | |
//* Force Full-Width Layout | |
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' ); | |
//* Remove Genesis breadcrumbs | |
remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' ); | |
//* Add Noindex tag to the page | |
add_action( 'genesis_meta', 'b3m_noindex_page' ); | |
function b3m_noindex_page() { | |
echo '<meta name="robots" content="noindex, follow">'; | |
} | |
//* Insert Google CSE code into <head> section of webpage | |
add_action( 'genesis_meta', 'b3m_google_cse_meta', 15 ); | |
function b3m_google_cse_meta() { ?> | |
<script> | |
(function() { | |
var cx = '007934345622138067308:qqlbijy4dni'; | |
var gcse = document.createElement('script'); | |
gcse.type = 'text/javascript'; | |
gcse.async = true; | |
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx; | |
var s = document.getElementsByTagName('script')[0]; | |
s.parentNode.insertBefore(gcse, s); | |
})(); | |
</script><?php | |
} | |
//* Add custom body class | |
add_filter( 'body_class', 'b3m_add_body_class' ); | |
function b3m_add_body_class( $classes ) { | |
$classes[] = 'google-cse'; | |
return $classes; | |
} | |
//* Remove standard Genesis loop and insert our custom page content | |
remove_action( 'genesis_loop', 'genesis_do_loop' ); | |
add_action( 'genesis_loop', 'b3m_custom_content' ); | |
function b3m_custom_content() { ?> | |
<?php if ((isset($_REQUEST['q'])) && (!empty($_REQUEST['q']))) { | |
$query = 'Search Results for: ' . $_REQUEST['q']; | |
} | |
else { | |
$query = 'Search Results for: '; | |
} ?> | |
<div itemtype="http://schema.org/SearchResultsPage" itemscope="itemscope"> | |
<div class="archive-description"> | |
<h1 class="archive-title"><?php echo($query) ?></h1> | |
</div> | |
<div class="entry"> | |
<?php echo get_the_content(); ?> | |
<gcse:searchresults-only></gcse:searchresults-only> | |
</div> | |
</div> | |
<?php } | |
//* Run the Genesis loop | |
genesis(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 67-68 should use
get_search_query()
, instead of$_REQUEST['q']
, asget_search_query()
is escaped and much safer.