October 6th, 2008 at 8:01 pm

OpenSearch Wordpress Plugin

Jump to Comments
Submit to del.icio.us

I’ve been searching for a plugin for Wordpress that makes an OpenSearch file.

Found nothing so i’ve been working on it myself, however lately I’ve seen another OpenSearch plugin appear in the plugins database.

The plugin system of wordpress is ok, but could be a lot better, namely a class system. The way it works now is there are a lot of events that you can register functions to, that will fire when the event happens. You can also create some filters that will for example pass the text that has been written in a new post to alter some text like tags added to the post.

It would have been very nice if the plugin system would have worked with a class system, by simply creating a class to expand on the features.

But enough talk about the plugin system, let me tell you some about the plugin. It’s my first plugin so I’m still experimenting with the API. The way it works now is it creates a OpenSearch.xml file when the plugin activates and the file is basically static with some variable values that make the file specific for your blog.

As I said before, now someone else started working on the plugin so mine has become useless as the other one is superior to mine. So without further ado, the code that I had for anyone to download if they please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

/**
  Edits:
    Ezra      18-08-08    Eerste versie
    Ezra      19-08-08    Hij werkt, gzip ook!
    Ezra      28-08-08    Bij Deactivaten wordt het bestand verwijderd. Encoding veranderd naar US-ASCII.
 
*/


/*
Plugin Name: OpenSearch
Plugin URI: http://codedump.tsdme.nl
Description: Add a OpenSearch description to your blog
Version: 0.1
Author: Ezra (Ezra@tsdme.nl)
Author URI: http://codedump.tsdme.nl/Admin
*/


//Definitions
define("OS_LOCATION", "OpenSearch.xml");
define("PLUGIN_BORDER", "\t<!-- OpenSearch Plugin -->\n");

//Hooks
register_activation_hook(__FILE__, 'OS_Activate');
register_deactivation_hook(__FILE__, 'OS_Deactivate');
add_action('wp_head', OS_Head, 10, 0);

//Functions
function SO_add_pages(){
  add_options_page('OpenSearch', 'OpenSearch', 10, __FILE__, 'OS_Menu');
}
function OS_Activate(){
  clearstatcache();
 
  $os_file  = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\n";
  $os_file .= "<OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\">\n";
  $os_file .= "\t<ShortName>".get_bloginfo('name')."</ShortName>\n";
  $os_file .= "\t<LongName>Search ".get_bloginfo('name')."</LongName>\n";
  $os_file .= "\t<Description>Search ".get_bloginfo('home')."</Description>\n";
  $os_file .= "\t<Tags>".get_the_tag_list('',',','')."</Tags>\n";
  $os_file .= "\t<Contact>".get_bloginfo('admin_email')."</Contact>\n";
  $os_file .= "\t<Url type=\"text/html\" template=\"http://gedachtegoed.tsdme.nl/?s={searchTerms}\"/>\n";
  $os_file .= "\t<Image height=\"48\" width=\"48\" type=\"image/png\">".get_bloginfo('home')."/favicon.png</Image>\n";
  $os_file .= "\t<Query role=\"example\" searchTerms=\"cat\" />\n";
  $os_file .= "</OpenSearchDescription>";
 
  $os_xml = @fopen(GetHomePath().OS_LOCATION, "wb");
  if($os_xml){
    flock($os_xml, LOCK_EX);
    fwrite($os_xml, $os_file);
    fclose($os_xml);
  }
  $gz_xml = @gzopen(GetHomePath().OS_LOCATION.".gz", "wb9");
  if($gz_xml){
    flock($gz_xml, LOCK_EX);
    gzwrite($gz_xml, $os_file);
    gzclose($gz_xml);
  }
}

function OS_Deactivate(){
  if(file_exists(GetHomePath().OS_LOCATION)){
    unlink(GetHomePath().OS_LOCATION);
  }
  if(file_exists(GetHomePath().OS_LOCATION.".gz")){
    unlink(GetHomePath().OS_LOCATION.".gz");
  }
}

function OS_Head(){
  echo PLUGIN_BORDER;
  echo "\t<link rel=\"search\" type=\"application/opensearchdescription+xml\" title=\"".get_bloginfo('name')." Search\" href=\"".get_bloginfo('url')."/".OS_LOCATION."\" />\n";
  echo PLUGIN_BORDER;
}

/**
   * Returns the path to the blog directory
   *
   * @since 3.0
   * @access public
   * @author Arne Brachhold
   * @return string The full path to the blog directory
  */

  function GetHomePath() {
   
    $res="";
    //Check if we are in the admin area -> get_home_path() is avaiable
    if(function_exists("get_home_path")) {
      $res = get_home_path();
    } else {
      //get_home_path() is not available, but we can't include the admin
      //libraries because many plugins check for the "check_admin_referer"
      //function to detect if you are on an admin page. So we have to copy
      //the get_home_path function in our own...
      $home = get_option( 'home' );
      if ( $home != '' && $home != get_option( 'siteurl' ) ) {
        $home_path = parse_url( $home );
        $home_path = $home_path['path'];
        $root = str_replace( $_SERVER["PHP_SELF"], '', $_SERVER["SCRIPT_FILENAME"] );
        $home_path = trailingslashit( $root.$home_path );
      } else {
        $home_path = ABSPATH;
      }

      $res = $home_path;
    }
    return $res;
  }
?>

Have fun with it and good luck to the person that is developing the other OpenSearch plugin!

Leave a Reply