This php snippet creates a search provider description according to OpenSearch Description specification and displays a button for adding your site to users browser as new search engine. View demo at aam.ugpl.de.

Change "Your Site Name" in the following code snippet into the name fo your site and "Your Site Slogan" into a short description of your site.

<?php
/**
* This php snippet creates a search provider description
* and displays a button to add your site to your browser as new search engine
*
* Works with drupal 5.x, 4.7.x, 4.6.x and 4.5.x
*
* works for IE 7.+ and Mozilla Firefox 2.+
*
* Snippet submitted by aam
*/

global $base_url;
$site_name = variable_get("site_name", "drupal");

$xmlfile ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xmlfile.="<OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\">\n";
$xmlfile.="<ShortName>Your Site Name</ShortName>\n";
$xmlfile.="<Description>Your Site Slogan</Description>\n";
$xmlfile.="<Url type=\"text/html\"\n";
$xmlfile.="   template=\"".$base_url."?q=search/node/{searchTerms}\"\n";
$xmlfile.="/>  \n";
$xmlfile.="</OpenSearchDescription>\n";

if ( ! file_exists("files/opensearchprovider.xml") ) {
  if (!$fp = fopen("files/opensearchprovider.xml", "w")) {
			print "<br><b>Error: Cannot open XML file for search provider description</b>\n"; }
		else {
			// Write content to the opened file.
			if (!fwrite($fp, $xmlfile)) {
				print "<br><b>Error: Cannot write to XML file</b>\n";
				}
			else {
				print "<br><b>Success: XML file search provider description written</b>\n";}
				//print "<h3>Following XML file written:</h3>\n";
				//print "files/opensearchprovider.xml";
				//readfile ("files/opensearchprovider.xml");
			fclose($fp);
			}
  }

$output .= "<p><form>\n"; 
$output .= "<fieldset><legend><b>Add this site as search provider to your browser</b></legend>\n"; 
$output .= "<p>". t("Want to add %site as a new search provider? Just click the button below!", array("%site" => $site_name )) ."</p>\n";
$output .= "<p><form><INPUT TYPE=\"button\" VALUE=\"Add Search Provider\" onClick='window.external.AddSearchProvider(\"".$base_url."/files/opensearchprovider.xml\");'></form></p>\n";
$output .= "<noscript>\n";
$output .= "<p><b>". t("WARNING") .":</b>". t("JavaScript is disabled in your browser.");
$output .= t("The sidebar requires JavaScript support to be enabled.") ."</p>\n";
$output .= "</noscript>\n";
$output .= "</fieldset>\n"; 

print $output;
?>

Comments

demon326’s picture

hi,thanx for the code but there is 1 other thing you can use so that the brows(ers) it can detect auto:

<link rel="search" type="application/opensearchdescription+xml"
  title="youre_site" href="files/opensearchprovider.xml" />

ad that code in youre header in page.tpl.php

Christefano-oldaccount’s picture

Rob Loach has created an OpenSearch Plugin module. A really nice touch is that it creates a base64 version of your site's favicon and inserts it into the opensearch.xml file.

If you want an opensearch plugin without a module, you can enable the OpenSearch Plugin module and disable it, and then point to your opensearch.xml file by putting the following in the <head> section of your theme's page.tpl.php file:

<link rel="search" type="application/opensearchdescription+xml" href="http://example.com/opensearch.xml" title="example.com" />
drupalina’s picture

All is good in Firefox, but in IE7 there is a problem.

I created a new page called "Useful Tools" and I inserted the above long code (and selected PHP for the input format). The button appears just like on http://aam.ugpl.de/?q=en/node/1363 but when in IE& I click on the button, nothing happens.

The error notification at the bottom-left cornet of IE7 tells me the following:

Line: 64
Char: 1
Error: Object doesn't support this property method
Code: 0
URL: http://aam.ugpl.de/?q=en/node/1363

On my own newly created "Useful Tools" page the error message reads the same, but with
Line: 213

In Firefox, when I go to that same "Useful Links" page, it gives me one error at the bottom. When I click on the details it reads:

this._windows[aWindow.__SSi] has no properties
var browsers = tabbrowser.browsers;

This same Firefox error message appears when I also visit http://aam.ugpl.de/?q=en/node/1363

nadersolutions’s picture

i created an .php file and copied the file but when i got the file i get an error

Fatal error: Call to undefined function variable_get() in /var/www/add.php on line 14

line 14

$site_name = variable_get("site_name", "drupal");

can anyone help please.