I recently migrated the vast majority of famvin.org sites in to a Network (multi-site) WordPress installation. There were, and continue to be, hitches along the way. And there were lessons learned that can be applied in other situations. For example, I use a child theme I developed for the site(s) based on WooThemes Headlines theme. The latest version of Headlines now includes the framework code to allow on to easily add custom options to a theme — and in particular to a child theme. Since child themes are my preferred customization method, this is a wonderful enhancement to the various themes from Woo that I find myself using.

The issue: When we moved to a network installation, one of the benefits was that one theme/child theme could be used for several sites, thereby simplifying upgrades and enhancements by allowing a “fix it once, apply it to all” way of working.  The down side was that I had to rethink some of my header customizations so that they used options instead of some sort of hard-coding that existed in the old multiple installation scenario. Keep in mind, the various sites using the theme are in various languages (english, french, italian, polish and spanish). The particular fix I am talking about here is to allow an individual site to use a specific custom Google Search if it is set up and revert to the standard search if not. Furthermore, the titles, text and responses need to be correctly translated into the appropriate site language.

What to do? The current Woo Framework makes it fairly easy to add options to the Theme options panels that are a part of the admin interface for the themes. So, after scouring the Woo Codex and forums, I added 4 new options to my theme:  woo_use_google_search (check box), woo_google_search_id (for the particular Google Custom Search ID), woo_google_results_page (the full URL for the page where search results are displayed) and woo_google_language (the site language so that it can be passed to Google and have google use the correct language in the results).  That language thing is a must for me, especially since one of the sites is in Polish, and far be it from me to successfully translate anything in to Polish!

Insuring that the parent theme was properly up to date so that I could use the woo_options_add() function, I proceded to add the following code to my functions.php in my child theme directory:

[php]function woo_options_add($options) {
$options[] = array( "name" => "Google Custom Search",
"type" => "heading");
$options[] = array( "name" => "Use Google Custom Search?",
"desc" => "Check here to use a Google Custom Search instead of the built in search",
"id" => "woo_use_google_search",
"std" => "",
"type" => "checkbox");
$options[] = array( "name" => "Google Custom Search ID",
"desc" => "Your Google Custom Search ID",
"id" => "woo_google_search_id",
"std" => "",
"type" => "text");
$options[] = array( "name" => "Results Page",
"desc" => "Your Google Search Results Page(full URL)",
"id" => "woo_google_search_results",
"std" => "",
"type" => "text");
$options[] = array( "name" => "Language",
"desc" => "Language code for your pages (en, es, it, pl, fr)",
"id" => "woo_google_language",
"std" => "",
"type" => "text");
return $options;[/php]

This created a new Options Tab named “Google Custom Search” in my Theme Options panel in the administrative interface for any site using the child theme. Sweet.

There are 2 files in the child theme folder that needed modification to make this have any effect: header.php and my very own template-search-results.php which controls the layout of the google search results page for a site.

A couple of decisions needed to be made:  what happens if there is no Custom Search engine appropriate for use on the site? and, what if the admin forgets to set a language? Case 1: the google custom search is only used if the check box “Use Google Search” is checked; and being anglo-centric, if you don’t give a language code, you get english.

With those decisions settled, the header.php was modified to include the following code:

[php]<!–?php if ( get_option(‘woo_search_disable’) <–> "true" ) : ?></pre>
<div id="search"><!–?php if ( get_option(‘woo_use_google_search’) == ‘true’ ) {  ?–>
<form id="cse-search-box" action="<?php echo get_option(‘woo_google_search_results’); ?>
<div><input type="hidden" name="cx" value="<?php echo get_option(‘woo_google_search_id’); ?>" />
<input type="hidden" name="cof" value="FORID:11" />
<input type="hidden" name="ie" value="UTF-8" />
<input id="s" type="text" name="q" value="<?php _e(‘Enter keywords…’, ‘woothemes’) ?>" /> onfocus="if (this.value == ‘<!–?php _e(‘Enter keywords…’, ‘woothemes’) ?–>’)
 {this.value = ”;}" onblur="if (this.value == ”)
{this.value = ‘<!–?php _e(‘Enter keywords…’, ‘woothemes’) ?–>’;}" />
<!–    <input name="q" size="31" type="text" />  –>
<input type="image" name="sa" value="Go" src="<?php bloginfo(‘template_directory’); ?>/images/ico-search.png" /></div>
<!–?php   } else { ?–>
<form id="searchform" action="<?php bloginfo(‘url’); ?>" method="get">
<input id="s" type="text" name="s" value="<?php _e(‘Enter keywords…’, ‘woothemes’) ?>" />  onfocus="if (this.value == ‘<!–?php _e(‘Enter keywords…’, ‘woothemes’) ?–>’)
{this.value = ”;}" onblur="if (this.value == ”)
{this.value = ‘<!–?php _e(‘Enter keywords…’, ‘woothemes’) ?–>’;}" />
<input type="image" value="Go" src="<?php bloginfo(‘template_directory’); ?>/images/ico-search.png" /></form>
<!–?php }  ?–></div>
<!– /#search –>
<!–?php endif; ?–>[/php]

This code decides if you want a search box or not. It then checks whether to use a google custom search or the standard search. If the google custom search is needed, it creates the search form with the appropriate parameters; if not, it simply posts the standard search box that came with the theme.

Now, fix the template for the search results… the post div of the template now looks like this:
<!–?php the_content(); ?–>

<!–?php $glang="en";<br ?–>
if (get_option(‘woo_google_language’) <> "") { $glang = get_option(‘woo_google_language’);} ?>
<form id="cse-search-box" action="<?php echo get_option(‘woo_google_search_results’); ?>
/ ">
<input type="hidden" name="cx" value="<?php echo get_option(‘woo_google_search_id’); ?>" />

<input type="hidden" name="cof" value="FORID:11" />

<input type="hidden" name="ie" value="UTF-8" />

<input type="text" name="q" size="31" />

<input type="submit" name="sa" value="<?php _e(‘Search’, ‘woothemes’); ?>" /></div>
<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=<?php echo $glang; ?>"></script>

<script type="text/javascript">// <![CDATA[
var googleSearchIframeName = "cse-search-results";

var googleSearchFormName = "cse-search-box";

var googleSearchFrameWidth = 600;

var googleSearchDomain = "www.google.com";

var googleSearchPath = "/cse";
// ]]></script>

<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js">// <![CDATA[

// ]]></script></div>
<!– /.entry –>
<!– /.post –>[/php]

After saving these files, I have a situation where sites that fill in and save the Google search parameters use the google search and get results in with google using the appropriate language. And sites that don’t have or need that feature, get the standard WP search, in the appropriate language.


Pin It on Pinterest

Share This